Use the following class to answer the question below: public class Time { privat
ID: 3659484 • Letter: U
Question
Use the following class to answer the question below: public class Time { private int hrs; private int mins; private int secs; public Time() { //implementation not shown } public Time(int h, int m, int s) { //implementation not shown } //Resets time to hrs=h, mins=m, secs=s public void resetTime(int h, int m, int s) { //implementation not shown } //Advances time by one second public void increment() { //implementation not shown } //returns true if this time equals t, false otherwise public boolean equals(Time t) { //implementation not shown } //returns true if this time is earlier than t, false otherwise public boolean lessThan(Time t) { //implementation not shown } //Returns time as a String in the form hrs:mins:secs public String toString() { //implementation not shown } } Create a client/tester class that creates a time object with the default constructor and tests the increment method.Explanation / Answer
Time.java
public class Time {
private int hrs;
private int mins;
private int secs;
public Time() {
hrs = 0;
mins = 0;
secs = 0;
}
// implementation not shown }
public Time(int h, int m, int s) {
//Advances time by one second
hrs = h;
mins = m;
secs = s;
}
//Advances time by one second
public void increment() {
if(secs == 60) {
secs = 0;
mins++;
if(mins == 60) {
mins = 0;
hrs++;
if(hrs == 24)
hrs=0;
}
}
secs++;
}
//Returns time as a String in the form hrs:mins:secs
public String toString() {
return hrs + ":" + mins + ":" + secs;
}
}
Test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Time time = new Time();
System.out.println("initial time " + time.toString());
for(int i=0; i < 3955; i++)
time.increment();
System.out.println("After 3955 increments " + time.toString());
}
}
output:
Initial time 0:0:0
After 3955 increments 1:5:55