Java Reference
In-Depth Information
The definition of the method getCopy can also be written as:
public Clock getCopy()
{
Clock temp = new Clock(hr, min, sec);
return temp;
}
This definition of the method getCopy uses the constructor with parameters, described
below, to initialize the instance variables of the object temp .
Next, we give the definitions of the constructors. The default constructor initializes each
instance variable to 0 . Its definition is:
public Clock()
{
hr = 0;
min = 0;
sec = 0;
}
You can also write the definition of the default constructor using the method setTime as
follows:
public Clock()
{
setTime(0, 0, 0);
}
The definition of the constructor with parameters is the same as the definition of the
method setTime . It initializes the instance variables to the values specified by the user. Its
definition is:
public Clock( int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
 
Search WWH ::




Custom Search