Java Reference
In-Depth Information
Class Player
We begin with class
Player
, whose final specification is in Fig. 3.15.
According to the specification of the game, a player has a name, has a score, and
is playing at some level of difficulty. Based on this, we decide that the construc-
tor should have parameters for the player's name and the playing level, with the
score initially set to
0
.
It should not be necessary for a different part of the program to change these
three values, but it will be necessary to access them, so we provide getter meth-
ods.
During the game, the score will be incremented —which may cause a
change in the level— so we provide a method to increment the score.
Finally, we write function
toString
. Actually, we did not have
toString
initially but found out later that it would be useful. Almost always, function
toString
turns out to be useful, so put one in every class.
/**
An instance is the
Canvas
that contains the clock and status
*/
public class
Clock
extends
Canvas {
/**
The time on the clock.
*/
private
Time time;
/**
The width and height of the canvas
*/
private static final int
WIDTH= 250;
private static final int
HEIGHT= 250;
/**
Constructor: a clock with time
t */
public
Clock(Time t) {
time= t;
setBackground(
new
Color(255, 235, 222));
setSize( WIDTH, HEIGHT);
}
/** =
the time on the clock
*/
public
Time getTime()
{
return
time;}
/**
Set the time to
t
and repaint the
Canvas */
public void
setTime(Time t)
{ time= t; repaint(); }
public void
paint(Graphics g) {
Draw the face —circle and tick marks
;
Draw the hands
}
}
Figure 3.14:
Class
Clock
Search WWH ::
Custom Search