****must use JOptionPane**** Consider a class Time that represents a time of day
ID: 3624695 • Letter: #
Question
****must use JOptionPane****
Consider a class Time that represents a time of day.It has attributes for the hour and
minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59.
a. Write a default constructor that initializes the time to 0 hours, 0 minutes. b. Write a private method isValid(hour, minute) that returns true if the given
hour and minute values are in the appropriate range.
c. Write a method setTime(hour, minute) that sets the time if the given values are valid.
d. Write another method setTime(hour, minute, isAM) that sets the time if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAm is true if the time is an a.m. time and false otherwise.
Add two more constructors that are analogous to the setTime methods described in Parts c and d.
getTime24 returns a string that gives the time in 24-hour notation hhmm. For
example, if the hour value is 7 and the minute value is 25, return "0725". If the hour value is 0 and the minute value is 5, return "0005". If the hour value is 15 and the minute value is 30, return "1530".
getTime12 returns a string that gives the time in 12-hour notation h:mm xx. For example, if the hour value is 7 and the minute value is 25, return "7:25 am". If the hour value is 0 and the minute value is 5, return "12:05 am". If the hour value is 15 and the minute value is 30, return "3:30 pm".
Explanation / Answer
// Consider a class Time that represents a time of day.It has attributes for the hour and
public class Time
{
private int hour, minute;
// a. Write a default constructor that initializes the time to 0 hours, 0 minutes.
public Time()
{
hour = minute = 0;
}
// b. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the appropriate range.
private static boolean isValid(int hour, int minute)
{
return (hour >= 0 && hour <= 23) && (minute >= 0 && minute <= 59);
}
// c. Write a method setTime(hour, minute) that sets the time if the given values are valid.
public void setTime(int hour, int minute)
{
if(isValid(hour, minute))
{
this.hour = hour;
this.minute = minute;
}
}
// d. Write another method setTime(hour, minute, isAM) that sets the time if the given values are valid. The given hour should be in the range 1 to 12. The parameter isAm is true if the time is an a.m. time and false otherwise.
public void setTime(int hour, int minute, boolean isAM)
{
// PM adjustment - add 12 to hour if not AM
setTime(isAM?hour : hour+12, minute);
}
// Add two more constructors that are analogous to the setTime methods described in Parts c and d.
public Time(int hour, int minute)
{
setTime(hour, minute);
}
public Time(int hour, int minute, boolean isAM)
{
setTime(hour, minute, isAM);
}
// getTime24 returns a string that gives the time in 24-hour notation hhmm.
public String getTime24()
{
return String.format("%02d%02d", hour, minute);
}
// getTime12 returns a string that gives the time in 12-hour notation h:mm xx.
public String getTime12()
{
return String.format("%d:%02d "+hour >= 12? "pm":"am", hour%12, minute);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.