Java Reference
In-Depth Information
E7.
Consider class
Counter
of Fig. 4.15. Create a subclass in which the incre-
mentation is done by 2 instead of 1. Be sure to specify any methods you write
carefully.
E8.
Consider class
Counter
of Fig. 4.15. Create a subclass that changes the value
of the counter to
0
whenever it reaches
60
(this could be used to count minutes
on a clock). Thus, the range of values of this Counter is
0..59
. We call this
“counting mod
60
”. Be sure to specify carefuly any methods you write.
E9.
Consider class
Counter
of Fig. 4.15. Create a subclass in which increment-
ing is done “mod
n
”, where
n≥2
(see the previous exercise). The user should
give
n
as an argument in a constructor call. Be sure to specify any methods you
write carefully.
E10.
Consider class
Counter
of Fig. 4.15. Create a subclass in which the value
is interpreted as minutes and seconds, e.g. 125 is two minutes and five seconds.
The only change necessary is in method
toString
, which should return a string
that gives the counter in minutes and seconds.
E11.
Design and implement a class
Timer
, an instance of which contains hours,
seconds, and minutes. It should use three private fields to contain the hours, sec-
onds, and minutes, and each of these field should be some form of
Counter
(see
/**
A counter, which can be incremented
*/
public class
Counter {
private int
value= 0; //
the current value of the counter
/**
Constructor: a counter that starts at 0
*/
public
Counter (){ }
/**
= this counter
*/
public int
getCounter()
{
return
value; }
/**
Set this counter to
c */
public void
setCounter(
int
c)
{ value= c; }
/** increment the counter */
public void
click()
{ value= value + 1; }
/** =
this counter, as a String
*/
public
String toString()
{
return
"" + value }
}
Figure 4.15:
Class
Counter
Search WWH ::
Custom Search