Loading...
Loading...
A stopwatch app stores times either as hours, minutes and seconds or as a total number of seconds since midnight. Show every time as HH:MM:SS together with its total in seconds.
Write a class Time with fields hours, minutes and seconds, a constructor Time(int hours, int minutes, int seconds) and a constructor Time(int totalSeconds) that splits the total and calls the first with this(...). Add int toSeconds() and String label().
S x (a total in seconds) or T h m s.HH:MM:SS total, with two digits for each part.Example
Input
3 S 3661 T 9 5 0 S 0
Output
01:01:01 3661 09:05:00 32700 00:00:00 0
3661 seconds is 1 hour, 1 minute and 1 second.
Try the problem first. Open one hint at a time.
Hours are total / 3600, minutes are total / 60 % 60 and seconds are total % 60.
The arguments of this(...) can be expressions, so the whole constructor is one line.
String.format("%02d:%02d:%02d", ...) adds the leading zeros.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor