1 chapter 8 objects and classes lecture 2 prepared by muhanad alkhalisy
Post on 14-Dec-2015
213 Views
Preview:
TRANSCRIPT
1
Chapter 8 Objects and ClassesLecture 2
Prepared by Muhanad Alkhalisy
2
Garbage Collection
As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM.
3
Garbage Collection, cont
TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable.
Using Classes from the java Library
4
5
The Date ClassJava provides a system-independent encapsulation of date and time in the java.util.Date class. You can use the Date class to create an instance for the current date and time and use its toString method to return the date and time as a string.
java.util.Date
+Date()
+Date(elapseTime: long)
+toString(): String
+getTime(): long
+setTime(elapseTime: long): void
Constructs a Date object for the current time.
Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.
Returns a string representing the date and time.
Returns the number of milliseconds since January 1, 1970, GMT.
Sets a new elapse time in the object.
The + sign indicates public modifer
6
The Date Class ExampleFor example, the following code
java.util.Date date = new java.util.Date();
System.out.println(date.toString());
displays a string like Sun Mar 09 13:50:19 EST 2003.
7
The Random ClassYou have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). A more useful random number generator is provided in the java.util.Random class.
java.util.Random
+Random()
+Random(seed: long)
+nextInt(): int
+nextInt(n: int): int
+nextLong(): long
+nextDouble(): double
+nextFloat(): float
+nextBoolean(): boolean
Constructs a Random object with the current time as its seed.
Constructs a Random object with a specified seed.
Returns a random int value.
Returns a random int value between 0 and n (exclusive).
Returns a random long value.
Returns a random double value between 0.0 and 1.0 (exclusive).
Returns a random float value between 0.0F and 1.0F (exclusive).
Returns a random boolean value.
8
The Random Class ExampleIf two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed 3.
Random random1 = new Random(3);
System.out.print("From random1: ");
for (int i = 0; i < 10; i++)
System.out.print(random1.nextInt(1000) + " ");
Random random2 = new Random(3);
System.out.print("\nFrom random2: ");
for (int i = 0; i < 10; i++)
System.out.print(random2.nextInt(1000) + " ");
From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961
9
Displaying GUI ComponentsWhen you develop programs to create graphical user interfaces, you will use Java classes such as JFrame, JButton, JRadioButton, JComboBox, and JList to create frames, buttons, radio buttons, combo boxes, lists, and so on. Here is an example that creates two windows using the JFrame class.
10
Test Frame Example Output
11
12
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
Declare, create, and assign in one
statementreferenceframe1
: JFrametitle: width:height:visible:
animation
13
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width:height:visible:
Set title property
animation
14
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible:
Set size property
animation
15
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible: true
Set visible property
animation
16
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible: true
Declare, create, and assign in one
statementreferenceframe2
: JFrametitle:width:height:visible:
animation
17
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible: true
referenceframe2
: JFrametitle: "Window 2"width:height:visible:
Set title property
animation
18
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible: true
referenceframe2
: JFrametitle: "Window 2"width: 200height: 150visible:
Set size property
animation
19
Trace CodeJFrame frame1 = new JFrame(); frame1.setTitle("Window 1"); frame1.setSize(200, 150); frame1.setVisible(true); JFrame frame2 = new JFrame(); frame2.setTitle("Window 2"); frame2.setSize(200, 150); frame2.setVisible(true);
referenceframe1
: JFrametitle: "Window 1"width: 200height: 150visible: true
referenceframe2
: JFrametitle: "Window 2"width: 200height: 150visible: true
Set visible property
animation
20
Adding GUI Components to WindowYou can add graphical user interface components, such as buttons, labels, text fields, combo boxes, lists, and menus, to the window. The components are defined using classes. Here is an example to create buttons, labels, text fields, check boxes, radio buttons, and combo boxes.
21
GUI Components Example Output
22
top related