Answer:
public class Clock
{
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
public Clock ()
{
setTime (0, 0, 0);
}
public Clock (int hours, int minutes, int seconds)
{
setTime (hours, minutes, seconds);
}
public void setTime (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;
}
//Method to return the hours
public int getHours ( )
{
return hr;
}
//Method to return the minutes
public int getMinutes ( )
{
return min;
}
//Method to return the seconds
public int getSeconds ( )
{
return sec;
}
public void printTime ( )
{
if (hr < 10)
System.out.print ("0");
System.out.print (hr + ":");
if (min < 10)
System.out.print ("0");
System.out.print (min + ":");
if (sec < 10)
System.out.print ("0");
System.out.print (sec);
}
//The time is incremented by one second
//If the before-increment time is 23:59:59, the time
//is reset to 00:00:00
public void incrementSeconds ( )
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes ( ); //increment minutes
}
}
///The time is incremented by one minute
//If the before-increment time is 23:59:53, the time
//is reset to 00:00:53
public void incrementMinutes ( )
{
min++;
if (min > 59)
{
min = 0;
incrementHours ( ); //increment hours
}
}
public void incrementHours ( )
{
hr++;
if (hr > 23)
hr = 0;
}
public boolean equals (Clock otherClock)
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
}