Tải bản đầy đủ (.pdf) (17 trang)

Java - Trang ď Chap02

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (133.73 KB, 17 trang )

Programming Java

Classes & Methods

Incheon Paik

Java

1

Computer Industry lab.


Contents
?
?
?
?
?
?
?
?
?
?
?

Java

The Structure of Method
Static Methods & Static Variables
Instance Methods & Variables


The Integer Class
The New Operator
Garbage Collection
Other Wrapper Classes
The StringBuffer Class
Arrays of Objects
Command-Line Arguments
The System Class

2

Computer Industry lab.


The Structure of Method
Method
retType mthName(paramList) {
}

void displayInts (int array[])
{
}

Exception Definition of Method
retType mthName(paramList) throws exceptionList {
}

int parse(String s) throws Exc
eptionX {
}


Method Invocation
mthName(args);

Java

3

Computer Industry lab.


Static Methods & Variables
Invocation of Static Method
Clsname.mthName(args);

double x = Math.sin(3.12);

Access to Static Variable
clsName.varName

Class Aclass {

Object x

static int a;
int b;
… … ..

Java


Object y

a

same

a

b

different

b

}

x.a = 10; x.b = 20;

Class Aclass x = new Aclass();

y.a = 30; y.b = 40;

Class Aclass y = new Aclass();

Now, x.a = ?
4

x.b = ?
Computer Industry lab.



Static Methods & Variables
class AreaCircumference {
public static void main(String args[]) {
System.out.println(“Max of –8 and –4 is “ + Math.max(-8, -4));
System.out.println(“Min of –8 and –4 is “ + Math.min(-8, -4));
double radius = 5;
double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;
System.out.println(“Radius is “ + radius);
System.out.println(“Area is “ + area);
System.out.println(“Circumference is “ + circumference);
}
}

Java

5

Computer Industry lab.


Instance Methods & Variables
Invocation of Instance Method
double x = myObjA.somemth();

objRef.mthName.mthName(args);
class DollarAmount {
public static void main(String args[]) {
String s1 = “The total cost is $45.67”;

int i1 = s1.indexOf(‘$’);
String s2 = s1.substring(i1);

Find out this position
Get substring after ‘$’ character

System.out.println(s2);
} // end of main
} // end of class

Java

6

Computer Industry lab.


The Integer Wrapper Class
Wrapper Class : Corresponding class to the basic type: Number, Integer,
Float, Double,Boolean, Character, Byte, Short, Long
class StringToInt {
public static void main(String args[]) {
String s = “125”;
Integer obj = Integer.valueOf(s);
int i = obj.intValue();
i += 10;
System.out.println(i);
} // end of main

135


} // end of class

Java

7

Computer Industry lab.


The New Operator
new Operator
clsName objRef = new clsName(args);

MyClass x = new MyClass();

class IntegerTest {
public static void main(String args[]) {
Integer iobj1 = new Integer(5);
int i1 = iobj1.intValue();
i1 = 5

System.out.println(“i1 = “ + i1);
System.out.println(iobj1);

5

} // end of main
} // end of class


Java

8

Computer Industry lab.


Garbage Collection
?

Garbage Collector
? Automatic Memory Management

?

finalize Method
? Call the finalize method before the garbage collector reclaim
the memory
Provide the method to release the resources
? Programmer can remove the resources(ex:open files)
directly using finalize method which garbage collector
cannot reclaim.

?

Java

9

Computer Industry lab.



Garbage Collection
protected
protectedvoid
voidfinalize()
finalize()throws
throwsThrowable
Throwable{{
////......
}}
public class GcTest{
static {
System.out.println("Initialize");
}
protected void finalize() throws Throwable {
System.out.println("Clean Up");
}

How can we display Clean Up
public static void main(String args[]) {
message? => In Exercise
System.out.println("In main .... ");
GcTest x = new GcTest();
x = null;
System.gc(); // We need to call this point to see the "Clean Up"
}
}
Java


10

Computer Industry lab.


The Double Wrapper Class
class DoubleDemo {
public static void main(String args[]) {
// variable initialize
double d1 = 0;
double d2 = 0;
double d3 = d1 / d2;

Result :

System.out.println(d3);

NaN

System.out.println(Double.isInfinite(d3));

False

System.out.println(Double.isNaN(d3));

True

System.out.println(Double.MAX_VALUE);

1.797E308


System.out.println(Double.MIN_VALUE);

4.9E-324

System.out.println(Double.POSITIVE_INFINITY);
System.out.println(Double.NEGATIVE_INFINITY);

Infinity
-Infinity

}
}

Java

11

Computer Industry lab.


The StringBuffer Class
StringBuffer Constructor
StringBuffer()
StringBuffer(int size)
StringBuffer(String s)
class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer(30);

StringBuffer sb3 = new StringBuffer(“abcde”);
System.out.println(“sb1.capacity = “ + sb1.capacity());

16

System.out.println(“sb1.length = “ + sb1. length());

0

System.out.println(“sb2.capacity = “ + sb1.capacity());

30

System.out.println(“sb2.length = “ + sb1. length());

0

System.out.println(“sb3.capacity = “ + sb1.capacity());
System.out.println(“sb3.length = “ + sb1. length());

21

What will be append()
and reverse() ?

5

} // end of main
} // end of class


Java

12

Computer Industry lab.


Arrays of Objects
Declaration of Array
Make the Space for Array Element
class StringArray {
public static void main(String args[]) {
String array[] = new String[5];
array[0] = “String 0”;
array[1] = “String 1”;
System.out.println(array.length);
System.out.println(array[0]);
System.out.println(array[1]);
} // end of main
} // end of class

Java

13

Computer Industry lab.


Command-Line Argument
Declaration of Array

Make the Space for Array Element
class CommandLineArguments {

java CommandLineArguments 1 2 abcde

public static void main(String args[]) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
} // end of main
} // end of class

Java

14

Computer Industry lab.


The System Class
Runtime Environment

System.out.println( ****)
Static variable of PrintStream

Exit() Method
void exit(int code)

currentTimeMillis() Method
long currentTimeMillis()


Method Invocation
Void arraycopy(Object source, int sourceIndex,
Object destination, int destinationIndex, int size)
Java

15

Computer Industry lab.


Exercise
?

Step 1 (Read Input)
?

C Language
scanf(“%s”, name);

?

Java
BufferedReader charStream = New BufferedReader(new
InputStreamReader(System.in));
String data = charStream.readLine();

?

Step 2 (Read Input Double)

import java.io.*; ? You need to write this statement at top of your program
double d = Double.valueOf(charStream.readLine().trim()).doubleValue();

?

Step 3 Using System.in.read() and StringBuffer
StringBuffer buf = new StringBuffer();
String
while ((c = (char) System.in.read()) != '\r')
buf.append(c);
RandomAccessFile file = new RandomAccessFile(buf.toString(), "rw");

Java

16

Computer Industry lab.


Exercise
?

Step 4 (OptionPane of Swing)
?

?

Step 5 (Garbage Collection)
?


Java

Show Dialog and Get Input from GUI

Refer to this slide #9,10

17

Computer Industry lab.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×