Question
Java public class TheTime { private int hour; private int minute; private int second; /* Please add two constants below */ /* private ...; */
Java
public class TheTime {
private int hour;
private int minute;
private int second;
/* Please add two constants below */
/* private ...; */
/* private ...; */
private int MAX_HOUR= 23;
private int MAX_SEC=59;
/* Modify the constructor so that increments when a valid time is created */
public TheTime(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
// checks if the this object is valid
if(this.isValidTime()){
COUNT++;
}
/* ...; */
}
/*
* Please add the private variable that records the number of valid TheTime
* objects created during the runtime, and its corresponding getter method
*/
/* private ...; */
// declared as static as we need to keep track of number across objects
private static int COUNT=0;
public int getCount() { return COUNT; }
/* Setters and Getters */
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
public int getSecond() {
return second;
}
public void setSecond(int second) {
this.second = second;
}
/* Please fill in the following blanks */
public boolean isValidTime() {
// checks if the respective value is less than the limit
if(hour>MAX_HOUR || minute>MAX_SEC|| second>MAX_SEC){
return false;
}
return true;
}
public TheTime nextsecond() {
// checks if seconds is at 59 mark
if (second == 59) {
// make the seconds 0
second = 0;
// checks if the minutes is at 59 mark
if (minute == 59) {
// incremtns the hours
if (hour == 23) {
hour = 0;
} else {
hour++;
}
// makes minutes 0
minute = 0;
} else {
// incremtns minutes
minute++;
}
} else {
// incremtns seconds
second++;
}
return this;
}
public boolean isEqual(TheTime t) {
if(t.getHour()!=hour || t.getMinute()!=minute || t.getSecond()!=second){
return false;
}
return true;
}
// toString
public String toString() {
return String.format("%02d:%02d:%02d", hour, minute, second);
}
}
MyTime class
/*import necessary Classes here*/
public class MyTime {
public static void main(String[] args) { /*instantiate a TheTime Class*/ /* Loop that calculate the number of instantiations need to be done with 3 random numbers --
}
Add following statements in the My Time class 1. Instantiate a The Time object 2. Write a loop (while/for/do) to calculate the number of instantiations need to be done with 3 random numbers until the program generates the same object that in the The Time ClassStep 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