Java Reference
In-Depth Information
The dialog boxes that are displayed when the player is asked for information
are also objects, but of a class that already exists in the GUI package that we will
be using.
We list these objects:
clock, time, face, minute hand, hour hand
player, name, score, level
status, clock window, clock game, dialog box
There may be more objects to think of, but this is certainly enough to begin
with. We move on to the class-design phase. We start by designing the clock.
Specifying class Clock
It is common to draw pictures like the clock on a Canvas , using methods of
class Graphics , where class Canvas occurs in package java.awt . Therefore,
view the clock as an extension of class Canvas .
A clock always has a time, so it makes sense to have a constructor that is
given a time to display on the clock. We will design class Time in a moment.
It will be necessary to obtain and to change the clock time, so we provide
getter and setter methods for the time.
We also need a method paint , as usual, that will draw on the Canvas . We
collect these methods into the specification given in Fig. 3.10.
Note one thing in Fig. 3.10. We have added a return statement in method
getTime , which returns null . Although Fig. 3.10 contains only a specification,
it will soon be turned into the class itself. Before we write the method bodies, the
class should compile; it should be syntactically legal. And functions must have
return statements. We write such return statements in all our specifications.
/** An instance is a time (hours and minutes) */
public class Time {
/** Constructor: instance with h hours, m minutes */
public Time( int h, int m) { }
/** = the hours of this time */
public int getHours() { return 0; }
/** = the minutes of this time */
public int getMinutes() { return 0; }
/** = a representation " hours:minutes " of this Time */
public String toString() { return null ; }
/** = this Time equals t */
public boolean equals(Time t) { return false ; }
}
Figure 3.11:
Specification of class Time
Search WWH ::




Custom Search