# Free Response Question - Time¶

The following is part a of a free response question that has been studied at colleges and universities.

You will implement two unrelated methods for a `Time`

class that keeps track of the time using a 24 hour clock. Consider the code for the `Time`

class provided below.

**Part a.** Write the method `tick`

which increases the number of seconds by one. If the number of seconds is 60 it adds one to the number of minutes and resets seconds to 0. If the number of minutes is 59 it adds one to the number of hours and resets the number of minutes to 0. If the number of hours reaches 24 it should be reset to 0.

```
1/**
2 * Objects of the Time class hold a time value for
3 * a European‐style 24 hour clock.
4 * The value consists of hours, minutes and seconds.
5 * The range of the value is 00:00:00 (midnight)
6 * to 23:59:59 (one second before midnight).
7 */
8public class Time
9{
10 // The values of the three parts of the time
11 private int hours;
12 private int minutes;
13 private int seconds;
14
15 /**
16 * Creates a new Time object set to 00:00:00
17 * Do not change this constructor.
18 */
19 public Time()
20 {
21 this.hours = 0;
22 this.minutes = 0;
23 this.seconds = 0;
24 }
25 /**
26 * Constructor for objects of class Time.
27 * Creates a new Time object set to h:m:s.
28 * Assumes, without checking, that the parameter values are
29 * within bounds.
30 * For this task, you don't need to worry about invalid parameter values.
31 * Do not change this constructor.
32 */
33 public Time(int h, int m, int s)
34 {
35 this.hours = h;
36 this.minutes = m;
37 this.seconds = s;
38 }
39 /**
40 * Add one second to the current time.
41 * When the seconds value reaches 60, it rolls over to zero.
42 * When the seconds roll over to zero, the minutes advance.
43 * So 00:00:59 rolls over to 00:01:00.
44 * When the minutes reach 60, they roll over and the hours advance.
45 * So 00:59:59 rolls over to 01:00:00.
46 * When the hours reach 24, they roll over to zero.
47 * So 23:59:59 rolls over to 00:00:00.
48 */
49 public void tick()
50 {
51 // Part a: complete the tick() method
52 }
53 /**
54 * Add an offset to this Time.
55 * Rolls over the hours, minutes and seconds fields when needed.
56 */
57 public void add(Time offset)
58 {
59 // Part b: complete the add method
60 }
61 public String toString()
62 {
63 return pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
64 }
65 /**
66 * Returns a string representing the argument value, adding a leading
67 * "0" if needed to make it at least two digits long.
68 * Do not change this.
69 */
70 private String pad(int value)
71 {
72 String sign = "";
73 if (value < 0)
74 {
75 sign = "‐";
76 value = ‐value;
77 }
78 if (value < 10) {
79 return sign + "0" + value;
80 } else {
81 return sign + value;
82 }
83 }
84 }
```

**How to solve this problem**

The first thing to do is try to solve the examples by hand. The question tells us that when the value of minutes is 0, and seconds is 59 the method tick should result in minutes = 1 and seconds = 0. When the value of minutes is 59 and the value of seconds is also 59 and the method tick is called the number of hours should increase and the minutes reset to 0. If the number of hours reaches 24 it should be reset to 0.

Use conditionals (if statements) to check for each of these conditions and take the appropriate actions when each condition is true.

**Part a.** Write the method `tick`

which increases the number of seconds by one. If the number of seconds is 60 it adds one to the number of minutes and resets seconds to 0. If the number of minutes is 59 it adds one to the number of hours and resets the number of minutes to 0. If the number of hours reaches 24 it should be reset to 0. When you have finished writing the method, click “Run” to test your solution. The main method has code that will test your solution using several different times.

**Part b.** Write the method `add(Time offset)`

which adds the seconds together and makes sure the result is 59 or less (incrementing the minutes as needed), adds the minutes together and makes sure the result is 59 or less (increments the hours as needed), and adds the hours together (resetting the hours to 0 if it reaches 24). When you have finished writing the method, click “Run” to test your solution. The main method has code that will test your solution using several different times.