In: Computer Science
Complete the code to make the following main work
public class Time {
**put the solution here**
public static void main(String[] args){
Time a = new Time(15,10,30);
Time b = new Time(); // default to 15:00:00(the start time of our class!)
System.out.println(a); // should print out: 15:10:30
System.out.println(b); // should print out:
System.out.println(a.dist(b)); // print the difference in seconds between the two timestamps
} }
public class Time {
private int hours, minutes, seconds;
public Time(int hours, int minutes, int seconds) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
}
public Time() {
this(15, 0, 0);
}
@Override
public String toString() {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
public int dist(Time other) {
int totalSeconds = hours*3600 + minutes*60 + seconds;
int otherTotalSeconds = other.hours*3600 + other.minutes*60 + other.seconds;
return Math.abs(totalSeconds - otherTotalSeconds);
}
public static void main(String[] args) {
Time a = new Time(15, 10, 30);
Time b = new Time(); // default to 15:00:00(the start time of our class!)
System.out.println(a); // should print out: 15:10:30
System.out.println(b); // should print out:
System.out.println(a.dist(b)); // print the difference in seconds between the two timestamps
}
}
