Java Clock Assignment
- by Mike S
For my assignment we are suppose to make a clock. We need variables of hours, minutes, and seconds and methods like setHours/getHours, setMinutes/getMinutes, setSeconds/getSeconds. Now the parts of the assignment that I am having trouble on is that we need a addClock() method to make the sum of two clock objects and a tickDown() method which decrements the clock object and a tick() method that increments a Clock object by one second.
Lastly, the part where I am really confused on is, I need to write a main() method in the Clock class to test the functionality of your objects with a separate Tester class with a main() method.
Here is what I have so far...
public class Clock {
private int hr; //store hours
private int min; //store minutes
private int sec; //store seconds
//Default constructor
public Clock ()
{
setClock (0, 0, 0);
}
public Clock (int hours, int minutes, int seconds)
{
setTimes (hours, minute, seconds);
}
public void setClock (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;
}
}
public int getHours ( )
{
return hr;
}
public int getMinutes ( )
{
return min;
}
public int getSeconds ( )
{
return sec;
}
//Method to increment the time by one second
//Postcondition: 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 tickSeconds ( )
{
sec++;
if(sec > 59)
{
sec = 0;
tickMinutes ( ); //increment minutes
}
}
public void tickMinutes()
{
min++;
If (min > 59) {
min = 0;
tickHours(); //increment hours
}
}
public void tickHours()
{
hr++;
If (hr > 23)
hr = 0;
}
}