Section 11.1 Classes Practice - Time Class
In this practice 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.
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.
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.
Part b.
You have attempted of activities on this page.
