Java Reference
In-Depth Information
As in the case of the default constructor, you can write the definition of the constructor
with parameters using the method setTime as follows:
public Clock( int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
This definition of the constructor with parameters makes debugging easier, because only
the code for the method setTime needs to be checked.
DEFINITION OF THE Class Clock
Now that we have defined the methods of the class Clock , we can give the complete
definition of the class Clock . Before the definition of a method, we include comments
specifying the preconditions and/or postconditions.
Precondition: A statement specifying the condition(s) that must be true before the
function is called.
Postcondition: A statement specifying what is true after the function call is completed.
The definition of the class Clock is:
public class Clock
{
8
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
//Default constructor
//Postcondition: hr = 0; min = 0; sec = 0
public Clock()
{
setTime(0, 0, 0);
}
//Constructor with parameters, to set the time
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
public Clock( int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
//Method to set the time
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
//
sec = seconds
Search WWH ::




Custom Search