Write a java program that follows the guidelines written in the prompt below: Im
ID: 3719245 • Letter: W
Question
Write a java program that follows the guidelines written in the prompt below:
Implement a class that simulates a Clock. Your class must have the following features: 1. Three private int instance variables - hours, minutes, and seconds 2. Mutator methods for the three instance variables. Allow only valid values to be accepted for the hours (0-23), minutes (0-59) and seconds (0-59) mutator methods. If the mutators are called with values outside these ranges, use 0 for each value outside the range. So if a call is made as setHours (31), set the hours to 0; if a call is made to setMinutes (-2), set the minutes to 0. A call to setMinutes (100) sets the minutes to 0. 3. Constructor - a three parameter constructor that initializes hours, minutes, and seconds to the values of the appropriate arguments provided in that exact order. Allow only valid values for the hours (0-23), minutes (0-59) and seconds (0-59). If the arguments have values outside these ranges, use 0 for each value outside the range. Do this by delegating the job to the mutator methods. So, in your constructor code, you should call, separately, the mutators for the hours, minutes and seconds and set the instance variables using these mutators! 4. Accessor methods for the three instance variables. 5. An instance method tostring ) which takes no parameters and returns a String that is a representation of the Clock as a String: "3:22:34". 6. An instance method tick ) which increments a Clock object by exactly one second. The Clock time must always be valid. So if the current time is "3:59:59", a tick) call will result in the new time of 4:0:0'" To successfully implement these features, you will have to check the boundary conditions and ensure that your clock stays in a valid state. Some boundary values for the Clock object are: 0:0:0, 0:0:59, 0:1:0, 0:59:59, 1:0:0, 23:59:59. Note that 24:0:0 is not a valid Clock value.Explanation / Answer
Java Code:
class Clock {
private int hours, minutes, seconds; //variables
//constructor
public Clock(int hours, int minutes, int second)
{
setHours(hours);
setMinutes(minutes);
setSeconds(second);
}
//acceesors and mutators
public int getHours() {
return hours;
}
public void setHours(int hours) {
if(hours<0 || hours>23)
hours = 0;
this.hours = hours;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
if(minutes<0 || minutes>59)
minutes = 0;
this.minutes = minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
if(seconds<0 || seconds>59)
seconds = 0;
this.seconds = seconds;
}
//toString function
@Override
public String toString() {
return hours + ":" + minutes + ":" + seconds;
}
//tick function
public void tick()
{
//seconds edge condition
if(this.getSeconds() == 59)
{
this.setSeconds(0);
//minutes edge condition
if(this.getMinutes() == 59)
{
this.setMinutes(0);
//hours edge condition
if(this.getHours() == 23)
this.setHours(0);
else
this.setHours(this.getHours() + 1);
}
else
{
this.setMinutes(this.getMinutes() + 1);
}
}
else
this.setSeconds(this.getSeconds() + 1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.