Professional Documents
Culture Documents
Week 3 - Lectures 1 and 2 - Chapter 8
Week 3 - Lectures 1 and 2 - Chapter 8
CLASSES
1
8.6 USING CLASSES FROM THE JAVA LIBRARY
2
8.6.1 THE DATE CLASS
The Date class is found in the util package in the Java library.
To use we write import java.util.Date;
You can use the Date class to create an instance (object) for
the current date and time and use its toString method to
return the date and time as a string.
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.
3
THE DATE CLASS EXAMPLE
For example, the following code
4
8.6.2 THE RANDOM CLASS
the Random class found also in the util package is used to
generate random values of different types as shown in the
table below.
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. 5
THE RANDOM CLASS EXAMPLE
To use the Random class you need to first import it:
import java.util.Random;
int y = r.nextInt(10);
// y could be any integer value between 0 inclusive and 10 exclusive (i.e. between 0 and 9)
double z = r.nextDouble();
// z could be any double value between 0.0 inclusive and 1.0 exclusive
• A static variable (static data field) is a data field shared by all objects
of the class.
7
WHY DO WE USE STATIC VARIABLES?
• If you want all the instances (objects) of a class to share data, use static variables, also known
as class variables. A static variable stores its value in a memory location common to all
objects of the class.
• Because of this common location, if one object changes the value of a static variable, all
objects of the same class are affected.
• An example of when do we use static variable is if we need a counter to keep track of how
many objects are created.
• To declare static variables, constants, and methods, use the static modifier.
8
HOW TO ACCESS STATIC
VARIABLES
Remember that we can access an instance data
such as radius via a reference variable as in
myCircle.radius
An instance method (non static method) is invoked (called) using an instance (object) of
the class.
Example: myCircle.getArea( ); // objectReference.method( )
A static method can be called without creating an instance of the class. It can be invoked
by either an instance (object) of the class or using the class name. Assume we have a
static method named getNumberOfObjects( ) that has the following header:
public static int getNumberOfObjects ( )
Then we can call it in either one of the following two ways:
myCircle.getNumberOfObjects( ); // objectReference.method( )
or
Circle.getNumberOfObjects( ); // class.method( )
10
STATIC METHODS VERSUS INSTANCE METHODS
11
STATIC CONSTANTS
12
CIRCLE CLASS WITH A STATIC
DATA
public class Circle {
double radius ; // instance data (each Circle object has its own radius)
static int numberOfObjects = 0; // static data (shared by all Circle objects)
public Circle(double r) {
radius = r;
numberOfObjects++; // increase by 1 each time an object is created
}
public double getArea( ) { // instance method
return radius * radius * Math.PI;
}
public static int getNumberOfObjects( ) { // static method
return numberOfObjects;
}
13
} // end class Circle
APPLICATION / DRIVER CLASS
public class TestCircle {
}
OUTPUT
Before creating objects
The number of Circle objects is 0
After creating c1
The number of Circle objects is 1
The number of Circle objects is 1
After creating c2
The number of Circle objects is 2
The number of Circle objects is 2
The number of Circle objects is 2 15
UML DIAGRAMS
The static data and the static methods are underlined in the UML
Diagram.
• You can use the public visibility modifier for classes, methods,
and data fields to denote that they can be accessed from any other
classes.
18
8.8 (CONTINUED)
In the example below the data field x and the method m1( )
have public visibility, z and m3( ) have private visibility, and
y and m2( ) have default visibility.
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();
} } }
} } }
20
8.9 DATA FIELD ENCAPSULATION
21
8.9 (CONTINUED)
The need for get and set methods:
24
CLASS CIRCLE WITH PRIVATE DATA AND
GETTER AND SETTER METHODS
public class Circle {
if (newRadius >= 0)
radius = newRadius;
else
radius = 0;
return radius;
if (newRadius >= 0)
radius = newRadius;
else
radius = 0;
26
APPLICATION / DRIVER CLASS
public class TestCircle {
public static void main(String[] args) {
Note: if you write c1.radius = -2; in the main method then you will get a27
syntax error since radius is private in the class.