Loading...
Loading...
A shop's billing machine has a 24-hour clock that starts at 00:00. Setting it to an impossible time like 25:10 is refused, and moving it forward past midnight wraps around.
Write a class Clock with private hours and minutes, boolean setTime(int h, int m), long advance(long minutes) that returns how many midnights were passed, and String getTime(). advance throws an IllegalArgumentException for negative minutes.
set h m, add x or show.set: TIME HH:MM, or INVALID TIME h m, STILL HH:MM. add: TIME HH:MM, followed by , NEXT DAY for one midnight or , d DAYS LATER for more, or REFUSED: minutes cannot be negative. show: HH:MM.Example
Input
4 set 22 45 add 90 set 24 0 show
Output
TIME 22:45 TIME 00:15, NEXT DAY INVALID TIME 24 0, STILL 00:15 00:15
22:45 plus 90 minutes is 00:15 the next day; 24:00 is not a valid time.
Try the problem first. Open one hint at a time.
Turn the time into total minutes: hours * 60 + minutes + x, using long.
The new hours are total / 60 % 24, the new minutes are total % 60, and the days passed are total / 1440.
String.format("%02d:%02d", h, m) pads with a leading zero.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor