Java Reference
In-Depth Information
This chapter spent a considerable amount of time designing the
class
Clock
. In order to
design this class, first we identified the operations and determined that each operation
must be implemented using a method. We then identified the data members and their
types. We also identified which member should be
public
and which should be
private
. Following the syntax of a method heading, we specified the heading of each
method and briefly explained what the method should do. Some methods can be
implemented using a single line of code, while others may require a complicated
algorithm. In either case, an algorithm must be designed and documented to implement
a method. Typically, as shown in the programming example in Chapter 7, the algorithm
to implement a method can be written as a pseudocode, which is a mixture of English
and the Java language. The means of describing the algorithm is not as important as the
clarity of the algorithm. The algorithm should be sufficiently clear so that a programmer
can code the algorithm in Java without having to make any further decisions about how
to solve the problem.
One way to specify the design of the
class
Clock
is:
public class
Clock
{
//data members
private int
hr;
private int
min;
private int
sec;
8
//methods
public
Clock()
{
// default constructor
// set time to 0,0,0
}
public
Clock(
int
hours,
int
minutes,
int
seconds)
{
// constructor with parameters
// set time according to the parameters
}
public void
setTime(
int
hours,
int
minutes,
int
seconds)
{
// set time according to the parameters
}
public int
getHours()
{
// return hr
}
Search WWH ::
Custom Search