class Time2 - Exercise 8.7

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
1,072
Modify class \(\displaystyle \text{Time2}\) of Fig. \(\displaystyle 8.5\) to include a tick method that increments the time stored in a \(\displaystyle \text{Time2}\) object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. Write a program that tests the tick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:

\(\displaystyle \bold{a)}\) incrementing into the next minute,
\(\displaystyle \bold{b)}\) incrementing into the next hour and
\(\displaystyle \bold{c)}\) incrementing into the next day (i.e., \(\displaystyle 11:59:59 \ \text{PM}\) to \(\displaystyle 12:00:00 \ \text{AM}).\)
 
Modify class \(\displaystyle \text{Time2}\) of Fig. \(\displaystyle 8.5\) to include a tick method that increments the time stored in a \(\displaystyle \text{Time2}\) object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. Write a program that tests the tick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:

\(\displaystyle \bold{a)}\) incrementing into the next minute,
\(\displaystyle \bold{b)}\) incrementing into the next hour and
\(\displaystyle \bold{c)}\) incrementing into the next day (i.e., \(\displaystyle 11:59:59 \ \text{PM}\) to \(\displaystyle 12:00:00 \ \text{AM}).\)
show us your effort/s to solve this problem.
 
To be able to modify class \(\displaystyle \text{Time2}\), we have first to know what is inside this class. Well class \(\displaystyle \text{Time2}\) contains the following:

\(\displaystyle \bold{1.}\) Three variables, \(\displaystyle \text{hour}, \text{minute},\) and \(\displaystyle \text{second}\).
\(\displaystyle \bold{2.}\) Five overloaded constructors.
\(\displaystyle \bold{3.}\) \(\displaystyle \text{setTime}\) method that calls three set methods.
\(\displaystyle \bold{4.}\) Two string methods, each one of them calls the same three get methods.

The main idea of this class is that you give it time, say, \(\displaystyle \text{Time2(h,m,s)} = \text{Time2(22,15,43)}\).

If the time is invalid, it will alert you that invalid data was entered.
If the time is valid, it will tell you:
Time is:
\(\displaystyle \text{22:15:43} \ (\text{10:15:43} \ \text{PM})\)

The modification idea (which is the OP question) is to create a \(\displaystyle \text{tick}\) method that lets time pass.
For example if you give this time, \(\displaystyle \text{Time2(h,m,s)} = \text{Time2(23,59,56)}\), it tells you that the time after \(\displaystyle 4\) ticks (or \(\displaystyle 4\) seconds) is:
\(\displaystyle \text{23:59:57} \ (\text{11:59:57} \ \text{PM})\)
\(\displaystyle \text{23:59:58} \ (\text{11:59:58} \ \text{PM})\)
\(\displaystyle \text{23:59:59} \ (\text{11:59:59} \ \text{PM})\)
\(\displaystyle \text{00:00:00} \ (\text{12:00:00} \ \text{AM})\)

The idea of this program is very simple, but implementing the idea with coding is very difficult. Therefore, we'll start slowly from the very bottom.
I am expecting that we will finish coding this program in three months.

💪🧑‍🎄
 
This is a warming class \(\displaystyle \text{Time2}\) I created. Every variable was assigned a value explicitly. I created an object \(\displaystyle \text{time}\) from class \(\displaystyle \text{Time2}\) to print the time.

Here is the code with a screenshot:

public class Time2 {
int hour = 15;
int minute = 15;
int second = 15;

public static void main(String[] args){
Time2 time = new Time2();
System.out.println("Time is: " +time.hour+":"+time.minute+":"+time.second);
}
}

Time2.png

Until now we did not code the other format of the time which is \(\displaystyle 15:15:15 = 3:15:15 \ \text{PM}\). Next time, we will develop our code to include a constructor so that we can pass the time data through it instead of explicitly assign it to the variables.
 
We developed our code to include a constructor \(\displaystyle \text{Time2(hour,minute,second)}\) and we also added a new method \(\displaystyle \text{toString}\) to print the time in the standard-format \(\displaystyle \text{AM/PM}\) along with the \(\displaystyle 24\)-format.

To be able to get the time in the standard format \(\displaystyle \text{AM/PM}\), I used a technique called a ternary conditional operator:

condition ? value_if_true : value_if_false;

It's a common short way to replace the if, else statement in one line. The same condition can be written as:

if (condition){
value_if_true
} else {
value_if_false
}

Here is the code so far with a screenshot:

package Moon;

public class Time2 {
int hour;
int minute;
int second;

public Time2(int hour,int minute, int second){
this.hour = hour;
this.minute = minute;
this.second = second;
}

public String toString(){
return String.format( "%02d:%02d:%02d (%d:%02d:%02d %s)",
hour,minute,second,
( (hour == 0 || hour == 12) ? 12 : hour % 12 ),
minute, second, ( hour < 12 ? "AM" : "PM" ) );
}

public static void main(String[] args){
Time2 time = new Time2(01,05,59);
System.out.println(time);
Time2 time2 = new Time2(15,15,15);
System.out.println(time2);
Time2 time3 = new Time2(99,99,99);
System.out.println(time3);
}
}

standard-format.png

As you can see in the picture, if you pass to our constructor correct time, it will happily show you the time, but it also does not care if the time is wrong something like \(\displaystyle \text{Time2(99,99,99)}\), it still takes it and outputs wrong time.

We will fix this problem in the next post. We will validate the constructor data by making sure that the time will have the following intervals:

\(\displaystyle 0 \leq \text{hour} < 24\)
\(\displaystyle 0 \leq \text{minute} < 60\)
\(\displaystyle 0 \leq \text{second} < 60\)

This will be the job of the three methods, \(\displaystyle \text{setHour}\), \(\displaystyle \text{setMinute}\), and \(\displaystyle \text{setSecond}\).
 
Our class \(\displaystyle \text{Time2}\) is ready😍

We tested the following:

\(\displaystyle \text{Time2(5,6,7)} = 05:06:07 \ \ \ (5:06:07 \ \text{AM})\)
\(\displaystyle \text{Time2(15,16,17)} = 15:16:17 \ \ \ (3:16:17 \ \text{PM})\)
\(\displaystyle \text{Time2(99,99,99)} = 99:99:99\). Wrong time.

I should also have tested the code for \(\displaystyle \text{Time2(0,6,7)} = 00:06:07 \ \ \ (12:06:07 \ \text{AM})\), but I was lazy!

Next time we are ready to modify class \(\displaystyle \text{Time2}\)💪🥴

Here is a screenshot to give you a taste of what we have done.

Time3.png
 
Top