Question: Java: The class should have one data field (an int with the number of seconds since midnight) instead of three. **This change should not affect

Java: 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.** Hello. Here is my code below. My code runs, as is, however, I am having trouble converting my int hour, int minute, int second into ONE int secondsSinceMidnight without the entire code breaking. Any help would be appreciated!!! ((my code is split. the top is the original code, the bottom is the test driver))

public class ModifiedTime2 { private int second; // 0 - 59 private int hour = second / 3600; // 0 - 23 private int minute = (second % 3600)/60; // 0 - 59

// Time2 no-argument constructor: // ensures that time2's clients can create time2 objects with default values // initializes each instance variable to zero public ModifiedTime2() { // no argument constructor this(0, 0, 0); // uses this in method-call syntax to invoke constructor with three arguments }

// declares Time2 constructor with a single int parameter: hour supplied, minute and second defaulted to 0 public ModifiedTime2(int hour) { this(hour, 0, 0); // invoke constructor with three arguments }

// Time2 constructor: hour and minute supplied, second defaulted to 0 public ModifiedTime2(int hour, int minute) { this(hour, minute, 0); // invoke constructor with three arguments }

// Time2 constructor: hour, minute and second supplied public ModifiedTime2(int hour, int minute, int second) { // this constructor validates and initializes the instance variables 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; }

// Time2 constructor: another Time2 object supplied public ModifiedTime2(ModifiedTime2 time) { // declares a time2 constructor that receives a reference to another time2 object // invoke constructor with three arguments this(time.hour, time.minute, time.second); // directly accesses the hour, min, second values of the argument time with the expression time. }

// Set Methods // set a new time value using universal time; // validate the data public void setTime(int hour, int minute, int second) { // throws an IllegalArgumentException if any are out of range 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"); } // sets time2's instance variables to the argument values 66-68 this.hour = hour; this.minute = minute; this.second = second; }

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

this.hour = hour; }

// validate and set minute public void setMinute(int minute) { if (minute < 0 || minute >= 60) { throw new IllegalArgumentException("minute must be 0-59"); }

this.minute = minute; }

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

this.second = second; }

// Get Methods // get hour value public int getHour() {return hour;}

// get minute value public int getMinute() {return minute;}

// get second value public int getSecond() {return second;}

// convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format( "%02d:%02d:%02d", getHour(), getMinute(), getSecond()); // methods toUniversalString and to String call getHour, getMin, getSecond } // Return the number of seconds since midnight as a long public long getSecondsSinceMidnight() { return (hour*3600 + minute*60 + second); } // convert to String in standard-time format (H:MM:SS AM or PM) public String toString() { return String.format( "%d:%02d:%02d %s (%d seconds since midnight) ", ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ), getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ), getSecondsSinceMidnight()); } // end method toString } // end class ModifiedTime2

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class Driver_ModifiedTime2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter hours:"); int hour = input.nextInt(); System.out.println("Please enter minutes:"); int minute = input.nextInt(); System.out.println("Please enter seconds:"); int second = input.nextInt(); ModifiedTime2 t = new ModifiedTime2 (hour, minute, second); System.out.printf("%s ", t.toString()); } // end main method } // end class Driver_ModifiedTime2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!