0% found this document useful (0 votes)
89 views34 pages

Java Packages & Classes Guide

The document discusses Java packages and classes. It explains that packages are used to group related classes and interfaces. The java.lang package contains fundamental classes like Object, String, Integer, and Double. Other classes discussed include Vector, Stack, and Scanner. Examples are provided to demonstrate using various methods of these classes like add(), pop(), and next().

Uploaded by

Dragan Ivanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views34 pages

Java Packages & Classes Guide

The document discusses Java packages and classes. It explains that packages are used to group related classes and interfaces. The java.lang package contains fundamental classes like Object, String, Integer, and Double. Other classes discussed include Vector, Stack, and Scanner. Examples are provided to demonstrate using various methods of these classes like add(), pop(), and next().

Uploaded by

Dragan Ivanov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Package and Some Classes

Declaration of Package
Usage of Package
Package of Java Language
What is Package ?

 Way to group a related class and interface into


one unit
 To resolve the name conflicts between class
names
Declaration of Package

package PackageName ;

 Declare at the beginning of source file


 [[Link]], [[Link]]

MyTest

Prints

[Link] [Link]
Usage of Package

 Use of absolute path name


 Declare to describe the full package name
 To resolve conflicts between class names

[Link].Print1 m1;
[Link].Print2 m2;

 [[Link]]
Use of import Statement

import [Link] ;

 Include all the classes in package


 import packageName.*;

import [Link].*;

 [[Link]]
[Link] Package

 Basic class to enlarge the function of Java

 Provide classes according to primitive type


(wrapper class)
 Integer class, Double class,. ..
[Link] Class

Object
Wrapper Class

Number System
Character String
Integer

Long Boolean StringBuffer

Float
Double
Object Class
 Super class of all classes
 All classes are to inherit the methods of Object class
 Method
 Object clone() : To copy the object equally
 boolean equals(Object obj) :
 Compare the object as parameter with the object which calls this method
 int hashCode() : Calculate the hash code value of the object
 Class getClass() : To get Class object corresponding to object
 String toString() : Convert the object into string
 [Ex 8.5]
 wait(), notify() : To control the status of thread
Wrapper Class

 Classes correspond to primitive type


 int -> Integer, char -> Character, double -> Double

 Reasons
 Role of home for constants and methods for each type
 When we need to deal with primitive type as object
Number Class

 Abstract Class
 Super Class of Integer, Long, Float, Double

 Method
 abstract int intValue() : Convert into int type
 abstract long longValue() : Convert into long type
 abstract float floatValue() : Convert into float type
 abstract double doubleValue() : Convert into double type
Integer Class
 Constant
 public static final int MAX_VALUE = 2147483647
 public static final int MIN_VALUE = -2147483648
 Method
 static int parseInt(String s) :
 Convert a Number in String into int type
 static int parseInt(String s , int radix) :
 Convert a number in String into int type with radix
 static String toBinaryString(int i) :
 Convert into binary string form
 static String toHexString(int i) :
 Convert into hexadecimal string form
Integer Class

 [[Link]]

[Link](s); As it is static
[Link](i); method...
...
Double Class

 Constant
 public static final double MAX_VALUE
=1.79769313486231570e+308
 public static final double MIN_VALUE =
4.94065645841246544e-308
 public static final double NaN = 0.0 / 0.0
 public static final double NEGATIVE_INFINITY = -1.0 / 0.0
 public static final double POSITIVE_INFINITY = 1.0 / 0.0
Double Class

 the parameter is NaN or not.


 static boolean isInfinite(double v) :
 Check whether the parameter is infinite or not.
 static Double valueOf(String s) : Method
 static long doubleToLongBits(double value) :
 Convert the bits represented by double type into long type bit
pattern
 static boolean isNaN(double v) :
 Check whether
 Convert String into Double type
Boolean Class

 Constant
 public static final Boolean TRUE = new Boolean(true)
 public static final Boolean FALSE = new Boolean(false)
 Method
 Boolean(boolean b) :
 Constructor to create boolean object receiving the initial value b
 Boolean(String s) :
 Constructor to receive the string value "true“ or "false“
 boolean booleanValue() :
 Return the boolean value of object
 static boolean getBoolean(String name) :
 Return the boolean value of system attribute
 static Boolean valueOf(String s) :
 Return the Boolean value correspond to string s
Character Class
 Constant
 public static final int MAX_RADIX = 36
 public static final char MAX_VALUE =‘\ffff’
 public static final int MIN_RADIX = 2
 public static final char MIN_VALUE =’\0000’

 Method
 Character(char value) : Constructor to initialize the object as value
 char charValue() : Convert into char type
 static boolean isDigit(char ch) : Test whether is digit?
 static boolean isLetter(char ch) : Test whether is letter?
 static boolean isLetterOrDigit(char ch) : Return when it is letter or
digit.

 [[Link]]
String Class
 Method
 char charAt(int index) :
 Return the character at specific position in string
 int length() :
 Return the length of string
 String toUpperCase() :
 Convert the string into upper character
 String toLowerCase() :
 Convert the string into lower character
 String trim() :
 Remove the white space before/after string
 String substring(int beginIndex) :
 Make the sub string from beginIndex to the end of the string
StringBuffer Class

 Provide the string sequence operation


 Method
 StringBuffer append(Type obj) :
 Append the obj(to be changed into string) to string
 StringBuffer insert(int offset, Type obj) :
 Insert obj(to be changed into string) into specified position

Object String char[]


boolean char int
long float double
StringBuffer Class

String now = new [Link]().toString();


StringBuffer strbuf = new StringBuffer(now);
[Link](" : ").append([Link]).append('.');
[Link]([Link]());
System Class

 Class for Java Virtual Machine and the control and


security for OS system
 Define the standard input and output
 Field
 public static InputStream in :
 Stream to read data from standard input device
 public static PrintStream out :
 Stream to output data to standard output device
 public static PrintStream err :
 Standard Error Stream to output error message
[Link]

 We will be creating an object of Vector class and


performs various operation like adding, removing etc.
 Vector class extendsAbstractList and
implements List, RandomAccess, Cloneable,
Serializable.
 The size of a vector increase and decrease
according to the program.
 Vector is synchronized.
Methods in Vector

 add(Object o): It adds the element in the end


of the Vector
 size(): It gives the number of element in the
vector.
 elementAt(int index): It returns the element
at the specified index.
 firstElement(): It returns the first element of
the vector.
 lastElement(): It returns last element.
 removeElementAt(int index): It deletes the
element from the given index.
Methods in Vector class

 elements(): It returns an enumeration of the


element
 In this example we have also
used Enumeration interface to retrieve the
value of a vector. Enumerationinterface has
two methods.
 hasMoreElements(): It checks if this
enumeration contains more elements or not.
 nextElement(): It checks the next element of
the enumeration.
import [Link].*;
public class VectorDemo{
public static void main(String[] args){
Vector<Object> vector = new Vector<Object>();
int primitiveType = 10;
Integer wrapperType = new Integer(20);
String str = "tapan joshi";
[Link](primitiveType);
[Link](wrapperType);
[Link](str);
[Link](2, new Integer(30));
[Link]("the elements of vector: " + vector);
[Link]("The size of vector are: " + [Link]());
[Link]("The elements at position 2 is: " + [Link](2));
[Link]("The first element of vector is: " + [Link]());
[Link]("The last element of vector is: " + [Link]());
[Link](2);
Enumeration e=[Link]();
[Link]("The elements of vector: " + vector);
while([Link]()){
[Link]("The elements are: " + [Link]());
}
}
}
Example on Stack class
import [Link].*;

public class StackDemo{


public static void main(String[] args) {
Stack stack=new Stack();
[Link](new Integer(10));
[Link]("a");
[Link]("The contents of Stack is" + stack);
[Link]("The size of an Stack is" + [Link]());
[Link]("The number poped out is" + [Link]());
[Link]("The number poped out is " + [Link]());
//[Link]("The number poped out is" + [Link]());
[Link]("The contents of stack is" + stack);
[Link]("The size of an stack is" + [Link]());
}
}
Example on Scanner class
import [Link];
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Read string input for username


[Link]("Username: ");

String username = [Link]();


// Read string input for password
[Link]("Password: ");
String password = [Link]();
// Read an integer input for another challenge
[Link]("What is 2 + 2: ");
int result = [Link]();
if ([Link]("admin") && [Link]("secret") &&
result == 4) {
[Link]("Welcome to Java Application");
} else {
[Link]("Invalid username or password, access
denied!");
}
}
}
Example on Date Class
import [Link].*;

public class DateDemo{


public static void main(String[] args) {
Date d=new Date();
[Link]("Today date is "+ d);
}
}
[Link]
Example on File
import [Link].*;
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("[Link]",true);
BufferedWriter out = new BufferedWriter(fstream);
[Link]("Hello Java");
//Close the output stream
[Link]();
}catch (Exception e){//Catch exception if any
[Link]("Error: " + [Link]());
}
}
}
[Link] class
BufferedReader class

Return
Method Description
Type
read( ) int Reads a single character
read(char[] cbuf, int off, Read characters into a portion of an
int
int len) array.
Read a line of text. A line is
readLine( ) String
considered to be terminated by ('\n').
close( ) void Closes the opened stream.
import [Link].*;

public class ReadStandardIO{

public static void main(String[] args) throws IOException{

InputStreamReader inp = new InputStreamReader([Link])


BufferedReader br = new BufferedReader(inp);

[Link]("Enter text : ");

String str = [Link]();

[Link]("You entered String : ");

[Link](str);
}
}

You might also like