0% found this document useful (0 votes)
31 views8 pages

Chapter 3 Packages

The document discusses the java.lang package, which provides fundamental classes for Java programs. It describes key classes like Object, Math, String, StringBuffer, and wrapper classes. The Math class contains constants and static methods for common mathematical operations. The String and StringBuffer classes are used to manipulate and modify strings. Wrapper classes allow primitive types to be used as objects. An example program is provided to demonstrate the Angle and Trig classes.

Uploaded by

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

Chapter 3 Packages

The document discusses the java.lang package, which provides fundamental classes for Java programs. It describes key classes like Object, Math, String, StringBuffer, and wrapper classes. The Math class contains constants and static methods for common mathematical operations. The String and StringBuffer classes are used to manipulate and modify strings. Wrapper classes allow primitive types to be used as objects. An example program is provided to demonstrate the Angle and Trig classes.

Uploaded by

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

M.

El Dick - I2211

The java.lang package

PACKAGES
Chapter 3 M. El Dick - I2211

Overview of java.lang Classes of java.lang


 Provides
classes fundamental to the design of Java  Object
programs  Math
 String
 Does NOT have to be imported  StringBuffer
 import java.lang.*; (not necessary)  System
 The“Wrapper” Classes (list shown later)
 Other …

M. El Dick - I2211 M. El Dick - I2211


Class Object Class Math
 Superclass of all Java classes  The class is final, the constructor is private, methods
and constants are all static
 Defines a set of non-static methods shared by all
 Contains 2 constants:
objects:  public static final double E
 boolean equals(Object obj) double x = Math.PI;
 public static final double PI
 boolean result = obj1.equals(obj2);

 String toString()  Contains a set of static methods:


 obj1.toString();  int abs(int i)
 Class getClass()  long abs(long l) int j = Math.abs(i);
 float abs(float f)
 obj1.getClass();
 double abs(double d)
 Other…

M. El Dick - I2211 M. El Dick - I2211

Class Math (cont’d) Exercise


Math.cos(angle);//returns the cosine of angle  Class Angle
 1 field : « value »
Math.sqrt(num);//returns the square root of num  1 constructor : initialize field based on parameter
Math.min(var1, var2);//returns the smaller value  1 method : return maximum of cos between the current angle and
another one in parameter

 Other public static methods:  Class Trig


 truncation: ceil, floor and round  2 fields: «angle1 » and « angle2 »
 1 constructor: initialize fields based on 2 angles as parameters
 trigonometry: sin, cos, tan, asin, acos, atan, toDegrees,  1 method: return maximum of cos between the two angles of the
and toRadians current trig
 logarithms: log and exp  1 main method: create 2 Angle objects, 1 Trig object and displays
the result of the previous method
 others: sqrt, pow, max and random

M. El Dick - I2211 M. El Dick - I2211


Classes String and StringBuffer Class String
 String  Public constructors:
 Manipulate constant strings  String()
 StringBuffer  String(char[ ] data)
 Manipulate strings that can be modified  String(String original)
 String(StringBuffer buffer)
public static String reverse(String source){  Other…
int i, len = source.length();
StringBuffer dest = new StringBuffer(len); String word = “hello”;
for (i = (len-1); i >= 0; i--){
dest.append(source.charAt(i));
} Char table[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};
return dest.toString(); String word = new String(table);
}

M. El Dick - I2211 M. El Dick - I2211

Class String (cont’d) Class StringBuffer


 Public non-static methods:  Public constructors:
 String concat(String anotherString) or +  StringBuffer()
 int compareTo(String anotherString)  StringBuffer(int length)
 int compareToIgnoreCase(String anotherString)  StringBuffer(String str)
 char charAt(int index)
 int length()
 String toUpperCase(), toLowerCase()
StringBuffer buffer = new StringBuffer(word)
 String subString(int beginIndex )
 Other…

String up = word.toUpperCase()

M. El Dick - I2211 M. El Dick - I2211


Class StringBuffer (cont’d) Wrapper Classes
 Public non-static methods:
Primitive Type Wrapper Class
 StringBuffer append(String str), append(char c),
append(double d), etc. byte Byte
 char charAt(int index) short Short
 StringBuffer deleteCharAt(intindex) int Integer
 StringBuffer insert(int offset, char[] str), insert(int long Long
offset, char c), etc. float Float
 Other… double Double
char Character
String up = word.toUpperCase() boolean Boolean
void Void
M. El Dick - I2211 M. El Dick - I2211

Why do we need them? Creating wrapper object


 A wrapper object can be used in any situation where a
primitive value will not suffice Using its primitive value Using String
Integer age = new Integer(40); Integer age = new Integer(“40”);
 For example, Double n = new Double(8.2); Double n = new Double(“8.2”);
 Some objects serve as containers of other objects Boolean b = new Boolean(false); Boolean b = new Boolean(“false”);
 Primitive values could not be stored in containers, but
Character a = new N/A
wrapper objects could be
Character(‘&’);

 Some methods take an object as a parameter


 Primitive values cannot be used, but wrapper objects can
be used Cannot create objects of the wrapper class Void

M. El Dick - I2211 M. El Dick - I2211


Methods of wrapper classes Methods of wrapper classes
 To convert to another type  To compare 2 objects of the same class

Integer num = new Integer(4); Integer num1 = new Integer(4);


float flt = num.floatValue(); //stores 4.0 in flt Integer num2 = new Integer(11);
if(num1.compareTo(num2)< 0)
Double dbl = new Double(8.2); System.out.println(“Num 1 is
int val = dbl.intValue(); //stores 8 in val smaller than Num 2”);

M. El Dick - I2211 M. El Dick - I2211

Static methods of wrapper classes Constants of wrapper classes


 To convert a representation in a String to the  Class Integer
associated primitive:  MIN_VALUE: smallest int value
 MAX_VALUE : largest int value
int num1 = Integer.parseInt(“3”);

double num2 = Double.parseDouble(“4.7”);  Same applies for other numeric classes

long num3 = Long.parseLong(“123456789”);

M. El Dick - I2211 M. El Dick - I2211


Autoboxing Class System
 Automatic conversion of a primitive value to its wrapper  Static class
object:  Cannot create objects of System
 Static fields
Integer obj;
int num = 42;  public static InputStream in;
Integer obj;
obj = num;  public static PrintStream out;
int num = 42;
obj = new Integer(num);  public static PrintStream err;

 Reverse conversion (unboxing) also automatic


System.out.println(“The code is ”+number);

M. El Dick - I2211 M. El Dick - I2211

Class System Basic input/output


 Static methods import java.util.Scanner;
 public static void exit(int status);
public Class Test{
 Immediately terminate a program

 Return a status code


public static void main(String[] args) {
Scanner s = new Scanner(System.in);
By convention, a non-zero status code indicates an error System.out.printf(“Enter an integer : “);
System.exit(6); int number2 = s.nextInt ();

System.out.printf(“Enter many doubles: “);


 public static String getProperty(String key); while (s.hasNextDouble()) {
double number2 = s.nextDouble();
System.getProperty("os.name"); }
}
System.getProperty(“java.version"); }

M. El Dick - I2211 M. El Dick - I2211


Class Date
The java.util package import java.util.Date;

class DateDemo {
public static void main(String args[]) {
Date date = new Date(); // Get today's date

System.out.println(date);

long msec = date.getTime();


System.out.println("Milliseconds since Jan. 1, 1970 GMT = " + msec);

int month = date.getMonth();


int day = date.getDay();
System.out.println("Today is " + day + " " +month);
}
M. El Dick - I2211 } M. El Dick - I2211

Abstract class Calendar Exercice


import java.util.Calendar;
 Write a program that can
public class Main {  Compute the difference between two dates
public static void main(String[] args) {  Display it in terms of seconds
Calendar now = Calendar.getInstance(); // Get today's date

System.out.println("Current Year is : " + now.get(Calendar.YEAR));


// month start from 0 to 11
System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1));
System.out.println("Current Date is : " + now.get(Calendar.DATE));
}
}

M. El Dick - I2211 M. El Dick - I2211


import java.util.Calendar;

public class DateDifferent{

public static void main(String[] args){


Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2007, 01, 10);
calendar2.set(2007, 07, 01);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff / 1000;
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
}
}

M. El Dick - I2211

You might also like