Question: Java public class theTime { private int hour; private int minute; private int second; /*Please add two constants below*/ private int maxhour = 23; private
Java

public class theTime { private int hour; private int minute; private int second; /*Please add two constants below*/ private int maxhour = 23; private int maxminute = 60;//max second also private boolean isvalid ; /*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; isvalid = isValidTime(); if(isvalid == false) { System.out.println("reassign the valid values"); System.exit(0); } } /*Please add the private variable that records the number of valid TheTime objects created during the runtime, * and its corresponding getter method*/ private int validobj=0; { if(isvalid==true) validobj++; }
public int getnoofvalidobj(){ return validobj; } /*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() { if(this.hour=0 && this.minute=0 && this.second=0) { return true; } return false; } public theTime nextsecond() { theTime k = new theTime(-1,-1,-1); if(second==59) { k.setSecond(0); if(minute==59) { k.setMinute(0); if(hour==23) { k.setHour(0); }else { k.setHour(getHour()+1); } }else { k.setMinute(getMinute()+1); } }else { k.setSecond(getSecond()+1); k.setHour(getHour()); k.setMinute(getMinute()); } return k; }
public boolean isEqual(theTime t) { if(this.hour == t.getHour() && this.minute == t.getMinute() && this.second == t.getSecond()) { return true; }else { return false; } } //toString public String toString() { String format; if(isvalid==true) { if(this.hour=0) { format = "0"+String.valueOf(this.hour)+":"; }else { format = String.valueOf(this.hour)+":"; } if(this.minute=0) { format = format+String.valueOf(this.minute)+":"; }else { format = String.valueOf(this.minute)+":"; } if(this.second=0) { format = "0"+String.valueOf(this.second); }else { format = format+String.valueOf(this.second); } return format; }else { format = "the input time is not valid"; return format; } }
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 Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
