Java Reference
In-Depth Information
Specifying class Time
We now develop the specification of class Time (see Fig. 3.11). An instance
of Time contains a time on the clock. Such a time consists of an hour and a
minute, so the constructor of class Time will have parameters for them.
The user needs getter methods for the hour and minute. Should we also pro-
vide setter methods? We decide against this; if a new time is needed, it can be
obtained by creating a new instance of class Time . Thus, instances of class Time
are immutable objects; they cannot be changed.
What other methods are needed? The time on the clock is probably going to
be displayed somewhere, especially if the player gets it wrong. So we include
method toString , which will provide a printable version of the time.
Looking back, we wonder whether class Clock should have a method
toString . We decide that this is unnecessary. It would yield a String represen-
tation of the time on the clock, and this can already be obtained using get-
Time().toString() .
When we developed this program, we found out during the implementation
phase that it would be useful to have one more method in class Time . We dis-
covered the need to compare the time on the clock with the player's guess at the
time, so we added a method equals to class Time .
We have designed class Clock and class Time , which is used within Clock .
Looking at the other objects that have to do with the clock (the face and hands),
we decide that these objects will not have to be described by classes because they
will be simply drawn on the canvas of the clock. Thus, we have finished one
major portion of the design process.
Note that we did not discuss the fields we would need in classes Clock and
Time . The fields will be private, so they have nothing to do with the specifica-
tions of the classes, which deal with the users' view of the classes.
Specifying class ClockWindow
The diagram in the left of Fig. 3.9 contains a clock and the game status. This
window is what we have called object clockWindow . We now design class
ClockWindow to describe how this window behaves (see Fig. 3.12). To have an
instance appear as a window on your monitor, we make ClockWindow a subclass
of class JFrame .
The constructor for ClockWindow will be given a clock and the String
value that is to be placed in the status initially. The constructor's task is to add
the clock and status to the window and to ensure that the window is visible.
What else do we need? The time on the clock may be changed, but the clock
is given to the constructor. So the method that creates the ClockWindow has
access to the clock, and we see no need for getter/setter methods. The status will
also be changed, and we need to provide a setter method for it because otherwise
it would be unchangeable. That is about all we need in class ClockWindow.
Search WWH ::

Custom Search