1 chapter 8 objects and classes lecture 2 prepared by muhanad alkhalisy

22
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

Upload: laura-fordham

Post on 14-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

1

Chapter 8 Objects and ClassesLecture 2

Prepared by Muhanad Alkhalisy

Page 2: 1 Chapter 8 Objects and Classes Lecture 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.

Page 3: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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.

Page 4: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

Using Classes from the java Library

4

Page 5: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 6: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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.

Page 7: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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.

Page 8: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 9: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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.

Page 10: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

10

Page 11: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

Test Frame Example Output

11

Page 12: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 13: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 14: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 15: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 16: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 17: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 18: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 19: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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

Page 20: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

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.

Page 21: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

21

Page 22: 1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy

GUI Components Example Output

22