Lec_03_04_Classes
Lec_03_04_Classes
Programming in Java
Lecture 03 & 04 : Object Oriented Concepts and Classes
Overview
✓ Motivation
✓ Object Oriented Concepts
✓ Classes
✓ Constructors
✓ Declaring and Creating Objects
✓ Garbage Collection
✓ Instance Variables and Methods
✓ Static Variables, Constants and Methods
Motivations
ü After learning the preceding chapters, you are
capable of solving many programming problems using
selections, loops, methods, and arrays.
ü However, these Java features are not sufficient for
developing graphical user interfaces and large scale
software systems.
ü Suppose you want to develop a graphical user
interface as shown below. How do you program it?
OO Programming Concepts
Data Fields:
radius is _______
Methods:
getArea
✓ Objective:
✓ Demonstrate creating objects, accessing data, and using
methods.
Constructors
Circle() {
Constructors are a special kind
} of methods that are invoked to
construct objects.
Circle(double newRadius) {
radius = newRadius;
}
Constructors, cont.
new ClassName();
Example:
new Circle();
new Circle(5.0);
Default Constructor
ü ClassName objectRefVar;
ü Example:
ü Circle myCircle;
Declaring/Creating Objects in a Single Step
myCircle no value
yourCircle.radius = 100;
Trace Code, cont.
myCircle no value
Circle myCircle = new Circle(5.0);
: Circle
Circle yourCircle = new Circle();
radius: 5.0
yourCircle.radius = 100;
Create a
circle
Trace Code, cont.
Assign object
reference to
myCircle
Trace Code, cont.
yourCircle No values
Declares yourCircle
Trace Code, cont.
yourCircle No values
: Circle
Creates a new Circle
Object radius: 0.0
Trace Code, cont.
radius: 1.0
Trace Code, cont.
Change radius in
yourCircle : Circle
radius: 100.0
Caution
ü Recall that you use
Math.methodName(arguments) (e.g., Math.pow(3, 2.5))
radius = 1
Copying Variables of Primitive Data Types and
Object Types
Primitive type assignment i = j
Before: After:
i 1 i 2
j 2 j 2
Before: After:
c1 c1
c2 c2
ü 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.
The Date Class
java.util.Date
The + sign indicates
public modifer +Date() Constructs a Date object for the current time.
+Date(elapseTime: long) Constructs a Date object for a given time in
milliseconds elapsed since January 1, 1970, GMT.
+toString(): String Returns a string representing the date and time.
+getTime(): long Returns the number of milliseconds since January 1,
1970, GMT.
+setTime(elapseTime: long): void Sets a new elapse time in the object.
The Random Class
ü You 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() Constructs a Random object with the current time as its seed.
+Random(seed: long) Constructs a Random object with a specified seed.
+nextInt(): int Returns a random int value.
+nextInt(n: int): int Returns a random int value between 0 and n (exclusive).
+nextLong(): long Returns a random long value.
+nextDouble(): double Returns a random double value between 0.0 and 1.0 (exclusive).
+nextFloat(): float Returns a random float value between 0.0F and 1.0F (exclusive).
+nextBoolean(): boolean Returns a random boolean value.
Displaying GUI Components
Declare, create,
and assign in
frame1 reference one statement
JFrame frame1 = new JFrame();
frame1.setTitle("Window 1");
frame1.setSize(200, 150); : JFrame
frame1.setVisible(true); JFrame title:
frame2 = new JFrame(); width:
frame2.setTitle("Window 2"); height:
frame2.setSize(200, 150); visible:
frame2.setVisible(true);
Trace Code
frame1 reference
JFrame frame1 = new JFrame();
frame1.setTitle("Window 1");
: JFrame Set size
frame1.setSize(200, 150);
title: "Window 1" property
frame1.setVisible(true);
width: 200
JFrame frame2 = new JFrame();
height: 150
frame2.setTitle("Window 2");
visible:
frame2.setSize(200, 150);
frame2.setVisible(true);
Trace Code
frame1 reference
JFrame frame1 = new JFrame();
frame1.setTitle("Window 1"); : JFrame
frame1.setSize(200, 150); title: "Window 1"
frame1.setVisible(true); width: 200 Set visible
JFrame frame2 = new JFrame(); height: 150 property
frame2.setTitle("Window 2"); visible: true
frame2.setSize(200, 150);
frame2.setVisible(true);
Trace Code
frame1 reference
: JFrame
title: "Window 1"
width: 200
height: 150
JFrame frame1 = new JFrame();
visible: true
frame1.setTitle("Window 1");
frame1.setSize(200, 150); Declare, create,
frame1.setVisible(true); frame2 reference and assign in one
JFrame frame2 = new JFrame(); statement
frame2.setTitle("Window 2"); : JFrame
frame2.setSize(200, 150); title:
frame2.setVisible(true); width:
height:
visible:
Trace Code
frame1 reference
: JFrame
title: "Window 1"
JFrame frame1 = new JFrame(); width: 200
frame1.setTitle("Window 1"); height: 150
frame1.setSize(200, 150); visible: true
frame1.setVisible(true);
JFrame frame2 = new JFrame(); frame2 reference
frame2.setTitle("Window 2");
frame2.setSize(200, 150); Set title
: JFrame
frame2.setVisible(true); property
title: "Window 2"
width:
height:
visible:
Trace Code
frame1 reference
: JFrame
title: "Window 1"
width: 200
height: 150
JFrame frame1 = new JFrame(); visible: true
frame1.setTitle("Window 1");
frame1.setSize(200, 150);
frame1.setVisible(true); frame2 reference
JFrame frame2 = new JFrame();
frame2.setTitle("Window 2"); : JFrame
frame2.setSize(200, 150); title: "Window 2" Set size
frame2.setVisible(true); width: 200 property
height: 150
visible:
Trace Code
frame1 reference
: JFrame
title: "Window 1"
width: 200
height: 150
JFrame frame1 = new JFrame(); visible: true
frame1.setTitle("Window 1");
frame1.setSize(200, 150); frame2 reference
frame1.setVisible(true);
JFrame frame2 = new JFrame(); : JFrame
frame2.setTitle("Window 2"); title: "Window 2"
frame2.setSize(200, 150); width: 200 Set visible
frame2.setVisible(true); height: 150 property
visible: true
Instance Variables, and Methods
instantiate Memory
circle1
radius = 5 5 radius
UML Notation: numberOfObjects = 2
+: public variables or methods
underline: static variables or methods
Example of Using Instance and Class
Variables and Method
ü Objective:
ü Demonstrate the roles of instance and class variables and
their uses. This example adds a class variable
numberOfObjects to track the number of Circle objects
created.
Visibility Modifiers and Accessor/Mutator
Methods
ü Public
ü The class, data, or method is visible to any class in any package.
ü private
ü The data or methods can be accessed only by the declaring class.
ü The get and set methods are used to read and modify private
properties.
package p1; package p2;
public class C1 { public class C2 { public class C3 {
public int x; void aMethod() { void aMethod() {
int y; C1 o = new C1(); C1 o = new C1();
private int z; can access o.x; can access o.x;
can access o.y; cannot access o.y;
public void m1() { cannot access o.z; cannot access o.z;
}
void m2() { can invoke o.m1(); can invoke o.m1();
} can invoke o.m2(); cannot invoke o.m2();
private void m3() { cannot invoke o.m3(); cannot invoke o.m3();
} } }
} } }
ü To protect data.
Circle
The - sign indicates
private modifier -radius: double The radius of this circle (default: 1.0).
-numberOfObjects: int The number of circle objects created.
… Circle object 1