1. Create a java class named TimeType with three private integer data fields nam
ID: 3607572 • Letter: 1
Question
1. Create a java class named TimeType with three private integer data fields named hours, minutes, and seconds, and implement the following public methods: TimeType() no argument contructor. Sets all private data to 0. TimeType(int, int, int) constructor function. Set the private data according to the three parameters. The first parameter represents hours, the second minutes, and the third seconds. If any of the parameters are invalid, set all private data to e TimeType(TimeType) contructor function. Initializes the private data to the values stored in the TimeType parameter String toString() Returns the time stored in the object in the format HH:MM:SS AM/PM boolean Set(int,int,int) Sets the time in the object according to the three parameters. The first parameter represents hours, the second minutes, and the third seconds. If any of the parameters are invalid, does nothing and returns false. Otherwise, set the private data according to the three parameters and returns true. void Increase() Increases the time in the object by 1 SECOND. Adjust the minutes and hours as necessary void Decrease() Decreases the time in the object by 1 SECOND. Adjust the minutes and hours as necessary 2. After you have created and compiled the TimeType class, create a client Java application to test the TimeType class. This application, named TestTime, should do the following and display its results using a Scanner object: - Create a TimeType object called timel using the default constructor. - Create a TimeType object called time2 initialized to 23 hours, 59 minutes, and 58 seconds - Create a TimeType object called time3 initialized with the values from the time2 object. - Write the values of timel, time2, and time3 on separate lines - Attempt to set the time in timel to 25 hours, 0 minutes, and 0 seconds. If successful, display the time in time1 otherwise display the message Invalid Time". - Create a loop that will increase and write the time in time2 10 times - Create a loop that will decrease and write the time in time2 10 timesExplanation / Answer
Please find my implementation.
public class TimeType {
private int hour;
private int minute;
private int second;
public TimeType() {
hour = 0;
minute = 0;
second = 0;
}
public TimeType(int hour, int minute, int second) {
if(hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
hour = 0;
minute = 0;
second = 0;
}else{
this.hour = hour;
this.minute = minute;
this.second = second;
}
}
public TimeType(TimeType time) {
this.hour = time.hour;
this.minute = time.minute;
this.second = time.second;
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public void setHour(int hour) {
if(hour >= 0 && hour <= 59)
this.hour = hour;
}
public void setMinute(int minute) {
if(minute >= 0 && minute <= 59)
this.minute = minute;
}
public void setSecond(int second) {
if(second >= 0 && second <= 59)
this.second = second;
}
@Override
public String toString() {
String c = "AM";
if(hour > 11)
c = "PM";
return hour+":"+minute+":"+second+" "+c;
}
public void increase() {
second = second + 1;
minute = minute + second/60;
second = second%60;
hour = hour + minute/60;
minute = minute%60;
}
public void decrease() {
if(second > 0)
second = second -1;
else if(minute > 0){
minute = minute - 1;
second = 59;
}else if(hour > 0) {
hour = hour - 1;
minute = 59;
second = 59;
}
}
}
############
public class TimeTypeTest {
public static void main(String[] args) {
TimeType time1 = new TimeType();
TimeType time2 = new TimeType(23, 59, 58);
TimeType time3 = new TimeType(time2);
System.out.println(time1);
System.out.println(time2);
System.out.println(time3);
time1.setHour(25);
System.out.println(time1);
for(int i=0; i<10; i++){
time2.increase();
System.out.println(time2);
}
System.out.println();
for(int i=0; i<10; i++){
time2.decrease();
System.out.println(time2);
}
}
}
/*
Sample run:
0:0:0 AM
23:59:58 PM
23:59:58 PM
25:0:0 PM
23:59:59 PM
24:0:0 PM
24:0:1 PM
24:0:2 PM
24:0:3 PM
24:0:4 PM
24:0:5 PM
24:0:6 PM
24:0:7 PM
24:0:8 PM
24:0:7 PM
24:0:6 PM
24:0:5 PM
24:0:4 PM
24:0:3 PM
24:0:2 PM
24:0:1 PM
24:0:0 PM
23:59:59 PM
23:59:58 PM
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.