2. Consider a class Time that represents a time of day. It has attributes for th
ID: 3747124 • Letter: 2
Question
2. 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 isValidChour, minute) that returns true if the c. Write a method setTime(hour, minute) that sets the time if the given given hour and minute values are in the appropriate range. values are valid. CHAPTER 6/ More About Objects and Methods 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.Explanation / Answer
Please find the Java class Time.java below:-
1. Time.java
public class Time {
private int hour;
private int minutes;
private boolean isAM;
public Time(){
hour = minutes = 0;
}
private boolean isValid(int hour, int minute){
if (hour < 0 || hour > 23)
return false;
if (minute < 0 || minute > 59)
return false;
return true;
}
public void setTime(int hour, int minute){
this.hour = hour;
this.minutes = minute;
setTime(hour, minute, isAM);
}
public void setTime(int hour, int minute, boolean isAM){
if ((hour >= 1 && hour <= 12)){
this.hour = hour;
this.minutes = minute;
this.isAM = true;
}else{
this.isAM = false;
}
}
}
Please let me know in case of any clarifications required. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.