Java Reference
In-Depth Information
The implementation of class Time
A time consists of a number of hours and a number of minutes, and the eas-
iest and most obvious thing to do is to introduce private instance variables to con-
tain these two values. Based on the specification of the methods, their bodies are
easy to write. See Fig. 3.13. Often, developing the class specification takes more
time than the implementation.
The implementation of class Clock
Finally, we implement class Clock (see Fig. 3.14). The constructor has a
Time parameter, which gives the time on the clock, so we introduce a private
instance variable time to contain this Time . The constructor saves the parameter
in field time . It then does something that you probably would not have known
how to do: it sets the background color to a kind of pink and sets the size of the
Canvas that contains the clock. For this purpose, we introduce two constants to
contain the height and width.
The getter method for field time is easy to write. The setter method is also,
but it has the additional task of repainting the window because the time has been
The last method to implement is paint . We do not describe the stepwise
refinement of this method because our focus here is on object-oriented design,
not procedural design. Incidentally, this is the most difficult part of the whole
implementation. Take a look at the final program if you wish.
Implementation of class ClockWindow
We do not discuss the implementation of class ClockWindow because it con-
tains too many things with which you are not familiar. Take a look at it, if you
wish; you can get it from the CD.
Testing Clock, Time, and ClockWindow
You can use the following lines in DrJava to begin testing these three class-
es. The first line creates a new Time and stores its name in t ; the second line cre-
ates a new Clock and stores it in c ; and the third line creates a new ClockWindow
and stores it in g . Change the time on the first line and run again to look at a dif-
ferent clock.
/** An instance is a window with the clock and status */
public class ClockWindow extends JFrame {
/** Constructor: window with clock c and a status with value s */
public ClockWindow(Clock c, String s) { }
/** Set the value of the status to s */
public void setStatus(String s) { }
Figure 3.12:
Specification of class ClockWindow
Search WWH ::

Custom Search