Transcript Time1

CIS 270—Application
Development II
Chapter 8—Classes and
Objects: A Deeper Look
8.2.1 Time Class Case Study






Two classes: Time1 (Fig 8.1) and Time1Test (Fig.
8.2)
Time1 has three private instance variables and three
public methods (a.k.a., public services or the public
interface
__________).
Time1 has a default ___________
constructor that sets instance
variables to 0.
Note the code hour = ((h>=0 && h<24) ? h:0);
It means if(h>=0 && h<24) hour=h; else hour=0;
The Time1 object will always have __________
consistent data
(always in range), but not necessarily correct data.2
8.2.2 Time Class Case Study




Note the code String.format(“%02d:%02d:%02d",
hour, minute, second); in the Time1 class.
String
format is a method of the __________
class
The %02d means “format is leading 0, two decimal
integers” (this formatted output is new in J2SE 5.0)
The three formats (in “”) will be applied to hour,
minute, second

Likewise, note the code
String.format( "%d:%02d:%02d %s",
( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
minute, second, ( hour < 12 ? "AM" : "PM" ) );

The hour % 12 means hour ______
mod 12
3
8.2.3 Time Class Case Study





Note Time1 time = new Time1(); in the Time1Test
class.
Variable time is assigned the address of a ________
Time1
object (time is a reference variable).
The code System.out.println(
time.toUniversalString() ); prints the string
returned when the object time calls the method
toUniversalString() in the class _________
Time1 .
The code time.setTime( 13, 27, 6 ); is the
object time calling the setTime method in Time1.
The class MemberAccessTest in Fig. 8.3 shows that a
private members.
client cannot directly access _________
4
8.4 The this Reference







Note the file ThisTest.java in Fig. 8.4.
ThisTest.java has two classes (but only one can be
public, which contains the ________
main method here).
Compiling ThisTest.java will create two _______
.class files.
Parameter names for a constructor should NOT be
the same as the instance variable (______)
field names.
Variables in a method with the same names as fields
will shadow (hide) the fields.
In that case, you will need to refer to fields with the
this .
keyword _______
Accessing members within the same class does not
require the this reference (see Line 35).
5
8.5 Overloaded Constructors






Note class Time2 in Fig. 8.5.
overloaded constructors
A class can have several (__________)
signature (parameter list) of each is different.
if the ________
argument constructor.
public Time2() is a no-__________
this can be used as the first statement in a
constructor’s body to call another constructor.
reuse with less
This use of this allows for code ______
maintenance if a constructor needs to be changed.
The methods setTime, toUniversalString, and
toString use set and get methods instead of
accessing instance variables directly.
6
8.6 Default and No-Argument
Constructors




If a class has no constructors, the compiler creates a
default constructor that sets numeric fields to 0,
null
Boolean fields to false, and reference fields to ____.
If a class has constructors, the compiler will not
create a default constructor. The class will need a
no-argument constructor.
body will set
A no-argument constructor with no _____
fields to their default values.
A constructor is identified by Java because it has the
same name as the class and has no _______
return type.
7
8.7 Set and Get Methods






Classes provide public methods to allow clients to set
private instance variables.
and get ________
Set and get methods should only be used as needed.
Set methods are also called mutator methods and get
accessor methods.
methods are called _________
Set and get methods make private fields accessible,
but not _______.
public
Get methods can hide the actual data format from
the client when they return formatted data.
Set methods can protect fields from careless
validity checking.
attempts to modify values by using ______
8
8.8 Composition





A data member of a class can be a reference to an
object of another class.
______
This is known as composition or a has-a
_____ relationship.
Note that the Employee class in Fig. 8.8 uses
composition.
Employee has two Date objects as instance variables.
Modeling Language), composition
In UML (Unified _________
would be expressed as follows:
Employee
Date
9
8.9 Enumerations





An enumeration is a special kind of class which
constants
declares a set of _________.
static
enum types are final and __________.
Do not try to create an object of type enum.
An enum can have instance variables, constructors,
and methods, much like an ordinary class.
Enhanced for loop (new in J2SE 5.0)





int scores[] = { 87, 93, 92, 95, 88, 90 };
int var = 0;
for(int var : scores) // var holds each array value
totalScore += var;
See Fig. 8.10 and Fig. 8.11 for example of enum.
10
8.11 static Class Members



A static field (class variable) is shared by all objects.
Example: numOfItems in a class called Item:

public static int numberOfItems = 0;

In the constructor Item, include the code numberOfItems++;
A public static field can be directly accessed by an
class name:
object or with the _______



A ________
private static field can be accessed by calling a
public static method using the class name:


anItem.numberOfItems
Item.numberOfItems
Item.aMethod()
See Fig. 8.12 and Fig. 8.13 for examples.
11
8.12-13 static Import and
final Instance Variables


static Import
 By using code like import static java.lang.Math.*; ,
you can access the static methods of a class without using
the class name.
Math .sqrt(900.0)
 For example, sqrt(900.0) instead of ______
final Instance Variables
Least Privilege: code should be granted
 Principle of _______
only the amount of privilege and access that it needs.
 Instance variables can be made final—they can only be
set in a constructor and cannot be changed otherwise.
 private final int INCREMENT;
12
8.14-15 Some OO Concepts

Software reuse



framework for
Java is a language, but it is also a ___________
accessing the Java API (thousands of prewritten classes)
Software can be created by using prewritten components
Data abstraction and encapsulation




hiding classes hide their implementation
Information ______:
from clients
Data abstraction: clients focus on data, not on
implementation details
abstract data type (ADT) has data representation
An __________
and operations that manipulate the data
A class is an ADT—it has data and operations
13