THINK FIRST·CODE LATER

← All labs

Time: hide how the data is stored

Problem

Write a class Time and a public class TimeOfDay. Time stores the time of day in one private field: the number of seconds since midnight (0 … 86 399). Users of the class never see this — they use:

  • a constructor Time(int h, int m, int s) (values may overflow, e.g. 25 h wraps to 1 h);
  • getHours(), getMinutes(), getSeconds() computed from the field;
  • addSeconds(int s) (also negative) and tick() (+1 s), wrapping around midnight;
  • to24h() → 09:05:00 and to12h() → 9:05:00 AM (12:xx is PM, 0:xx is 12 AM).

main reads h m s delta and prints:

Start: 23:59:59 = 11:59:59 PM
Tick:  00:00:00
After 3725 s: 01:02:05 = 1:02:05 AM

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l10-4-data-1.txt

23 59 59 3725
What your program must print for l10-4-data-1.txt
Start: 23:59:59 = 11:59:59 PM
Tick:  00:00:00
After 3725 s: 01:02:05 = 1:02:05 AM

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l10-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l10-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.