Question
Instructions : Modify the following class so that it is immutable. Do this by identifying the mutators, and then rewrite these methods so that instead
Instructions: Modify the following class so that it is immutable. Do this by identifying the mutators, and then rewrite these methods so that instead of changing the object that invoked the method, they create a new object that reflects the requested change. You only need to rewrite those methods that will have new implementations. Do not change the public interface of the class.
/** A class to keep track of the time of day, to the nearest second.
*/
public class TimeOfDay
{
public static final int HOURS_PER_DAY = 24;
public static final int MINUTES_PER_HOUR = 60;
public static final int SECONDS_PER_MINUTE = 60;
/* The hours, minutes, and seconds that have
elapsed since midnight.
*/
private int hours, minutes, seconds;
/** Create a new object representing the time of day.
@param h the hour
@param m the minute
@param s the second
*/
public TimeOfDay (int h, int m, int s)
{
hours = h;
minutes = m;
seconds = s;
}
/** Get the hour represented by this object.
@return the hour
*/
public int getHour() {return hours;}
/** Get the minute represented by this object.
@return the minute
*/
public int getMinute() {return minutes;}
/** Get the second represented by this object.
@return the second
*/
public int getsecond() {return seconds;}
/** Advance the time by one second.
*/
public TimeOfDay tick()
{
seconds++;
if (seconds == SECONDS_PER_MINUTE) {
seconds = 0;
minutes++;
if (minutes == MINUTES_PER_HOUR) {
minutes = 0;
hours++;
if (hours == HOURS_PER_DAY) {
hours = 0;
}
}
}
return this;
}
/** Advance the time by a number of seconds
* @param amount the number of seconds to advance
*/
public TimeOfDay advance (int amount)
{
for (int i = 1; i <= amount; ++i) {
tick();
}
return this;
}
/** Calculate the number of seconds elapsed since midnight.
* @return the number of seconds elapsed since midnight
*/
public int seconds_elapsed ()
{
return (hours * MINUTES_PER HOUR + minutes)
* SECONDS_PER_MINUTE + seconds;
}
/** Calculate the number of hours elapsed since midnight.
* @return the number of hours elapsed since midnight
*/
public double hours_elapsed ()
{
return (double) seconds_elapsed ()
/ (double) (MINUTES_PER_HOUR * SECONDS_PER_MINUTE);
/** Advance the time of day by 12 hours.
*/
public TimeOfDay flip()
{
hours = (hours + 12) % HOURS_PER_DAY;
return this;
}
/** Determine if the time is in the morning or afternoon /
* evening.
* @return true if the time is before noon, and false otherwise
*/
public boolean isAm ()
{
return hours < 12;
}
/** Advance the time to the start of the next hour.
*/
public TimeOfDay topOfTheHour()
{
second = minute = 0;
hour = (hour == HOURS_PER_DAY 1)
? 0
: hour + 1;
return this;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started