Question: Consider a Time class that represents a point in time, such as 9 a . m . or 3 : 3 0 p . m

Consider a Time class that represents a point in time, such as 9 a.m. or 3:30 p.m. Complete the implementation of the following two constructors for the Time class in which the time is stored as an integer that represents the number of minutes since midnight. public class Time
{
private int minutesSinceMidnight;
/**
Initializes a Time object to represent midnight.
*/
public Time()
{
/* Your code goes here */
}
/**
Initializes a Time object to represent the given time.
@param hours the hours component of the time: 0-12
@param minutes the minutes component of the time: 0-59
@param period a string indicating the period of the day: "am" or "pm"
*/
public Time(int hours, int minutes, String period)
{
/* Your code goes here */
}
/**
Returns the hour component of the time.
@return the hours as a value between 0-12
*/
public int getHours()
{
int hours = minutesSinceMidnight /60;
if (hours >12){ hours = hours -12; }
else if (hours ==0){ hours =12; }
return hours;
}
/**
Returns the minutes component of the time.
@return minutes as a value between 0-59
*/
public int getMinutes()
{
return minutesSinceMidnight %60;
}
/**
Return the period of the day.
@return a String indicating "am","pm", "noon" or "midnight"
*/
public String getPeriod()
{
if (minutesSinceMidnight ==0){ return "midnight"; }
else if (minutesSinceMidnight ==720){ return "noon"; }
if (minutesSinceMidnight <720){ return "am"; }
else { return "pm"; }
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!