Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA. Modify the Time2 class (below) to implement the time as the number of seco

ID: 3821359 • Letter: J

Question

JAVA. Modify the Time2 class (below) to implement the time as the number of seconds since midnight. The class should have one data field (an int with the number of seconds since midnight) instead of three. This change should not affect the arguments , behavior , or output of the public methods .

Create a Driver class with a main method to test your Time2 class . This program should ask the user to input the number of hours, minutes, and seconds past midnight, creating a Time2 object and using the mutator methods . The program should then use the toString() method to print out the time.
https://pastebin.com/LJx0ggJW

**My working code: https://pastebin.com/jGKAHuKz**
Errors:

My code isn't displaying the time like in the expected result

CODELAB ANALYSIS: LOGICAL ERROR(S) Problems Detected: The contents of your standard output is incorrect. Given the following was entered from the keyboard: you displayed: Enter hours Enter minutes: Enter seconds instead of: Enter hours: Enter Enter seconds 12:00:00 AM Failed 4 out of 4 test runs. Failed Test Run #1 The contents of your standard output is incorrect. Interactive Session W Hide Invisibles Highlight: None Expected Result: Your Code's Actual Result: Enter hours 23. Enter minutes :59.* Ente r. hours :2 Enter minutes :59 Enter seconds:59 11:59:59 PM Enter seconds :59 Print Content Results Support Show Highlighted only

Explanation / Answer

There were minor issues in your code.

You were calling toString method but were not printing that string

you were not allowing 0 seconds to be entered while setting seconds.

you had a code duplication.

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

Time2 time = new Time2();

System.out.print("Enter hours:");
int hours = input.nextInt();
System.out.print("Enter minutes:");
int minutes = input.nextInt();
System.out.print("Enter seconds:");
int seconds = input.nextInt();
input.close();

time.setHour(hours);
time.setMinute(minutes);
time.setSecond(seconds);
System.out.println(time.toString());
}

public static class Time2 {

private int hour;
private int minute;
private int second;

public Time2() {
this(0, 0, 0);
}

public Time2(int hour) {
this(hour, 0, 0);
}

public Time2(int hour, int minute) {
this(hour, minute, 0);
}

public Time2(int hour, int minute, int second) {
setTime(hour, minute, second);
}

public Time2(Time2 time) {
this(time.getHour(), time.getMinute(), time.getSecond());
}

public void setTime(int hour, int minute, int second) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.hour = hour;
this.minute = minute;
this.second = second;
}

public void setHour(int hour) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
this.hour = hour;
}

public void setMinute(int minute) {
if (minute < 0 && minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
this.minute = minute;
}

public void setSecond(int second) {
if (second < 0 || second > 60)
throw new IllegalArgumentException("second must be 0-59");
this.second = second;
}

public int getHour() {
return hour;
}

public int getMinute() {
return minute;
}

public int getSecond() {
return second;
}

public String toUniversalString() {
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}

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

Sample run

Enter hours:0
Enter minutes:0
Enter seconds:0
12:00:00 AM

Enter hours:23
Enter minutes:59
Enter seconds:59
11:59:59 PM