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

INSTRUCTION : A skeleton copy of class ‘Time’ has been created, which is used by

ID: 3814560 • Letter: I

Question

INSTRUCTION:

A skeleton copy of class ‘Time’ has been created, which is used by the class TimeTracker.

The template has comments, indicated by //, which documents the classes

The template has instructions, indicated by /*   */. Follow the instructions to allow the application to compile and run. The output will be similar to below.

- Invoke the three argument constructor from the one argument and two argument constructors

- Create a method to increment the time by one second, the method name can be found in class TimeTracker. setSecond() must be called, the method must handle the rollover of second from 59 to 0.

- Create a method to increment the minute variable. This method must handle the rollover of minute from 59 to 0.

- Create a method to increment the hour variable. This method must handle the rollover of hour from 23 to 0.

- Write a looping mechanism in case 4 of the switch statement to you increment the number of seconds appropriately. (any of while, do…while, or for loop may be used)

OUTPUT EXAMPLE:

Enter the time

Hours: 12

Minutes: 34

Seconds: 45

Hour: 12 Minute: 34 Second: 45

Universal time: 12:34:45   Standard time: 12:34:45 PM

1. Add 1 second

2. Add 1 Minute

3. Add 1 Hour

4. Add seconds

5. Exit

Explanation / Answer

// Please find the Updated Solution: ----------------------------------- package timetracker; import java.util.Scanner; class Time { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 // Time no-argument constructor: initializes each instance variable // to zero; ensures that Time objects start in a consistent state public Time() { this(0, 0, 0); // invoke Time constructor with three arguments } // end Time no-argument constructor // Time constructor: hour supplied, minute and second defaulted to 0 public Time( int h ) { this(h, 0, 0); /* Invoke the three argument Time constructor for initialization */ } // end Time one-argument constructor // Time constructor: hour and minute supplied, second defaulted to 0 public Time( int h, int m ) { this(h, m, 0); /* Invoke the three argument Time constructor for initialization */ } // end Time two-argument constructor // Time constructor: hour, minute and second supplied public Time( int h, int m, int s ) { setTime(h, m, s); // invoke setTime to validate time } // end Time three-argument constructor // Time constructor: another Time object supplied public Time( Time time ) { // invoke Time constructor with three arguments this(time.getHour(), time.getMinute(), time.getSecond()); } // end Time constructor with Time argument // Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m, int s ) { setHour(h); // set the hour setMinute(m); // set the minute setSecond(s); // set the second } // end method setTime // validate and set hour public void setHour( int h ) { hour = (((h >= 0) && (h < 24)) ? h : 0); } // end method setHour // validate and set minute public void setMinute( int m ) { minute = (((m >= 0) && (m < 60)) ? m : 0); } // end method setMinute // validate and set second public void setSecond( int s ) { second = (((s >= 0) && (s < 60)) ? s : 0); } // end method setSecond // Get Methods // get hour value public int getHour() { return hour; } // end method getHour // get minute value public int getMinute() { return minute; } // end method getMinute // get second value public int getSecond() { return second; } // end method getSecond // Tick the time by one second /* Write header for method tick */ void tick() { /* Write code that increments the second by one, then determines whether */ /* the minute needs to be incremented. If so, call incrementMinute. */ /* Must call setSecond() and handle rollover */ this.setSecond(this.getSecond() + 1); if( this.getSecond() == 0 ) this.incrementMinute(); } // end method tick // Increment the minute /* Write header for method incrementMinute */ void incrementMinute() { /* Write code that increments the minute by one, then determines whether */ /* the hour needs to be incremented. If so, call incrementHour. */ /* Must call setMinute() and handle rollover */ this.setMinute(this.getMinute() + 1); if( this.getMinute() == 0 ) this.incrementHour(); } // end method incrementMinute // Increment the hour /* Write header for method incrementHour. */ void incrementHour() { /* Write code that increments the hour by one. */ /* Must call setHour() and handle rollover */ this.setHour(this.getHour() + 1); } // end method incrementHour // convert to String in universal-time format (HH:MM:SS) public String toUniversalString() { return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond()); } // end method toUniversalString // convert to String in standard-time format (H:MM:SS AM or PM) public String toString() { return String .format("%d:%02d:%02d %s", ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM")); } // end method toStandardString } // end class Time public class TimeTracker { public static void main( String args[] ) { Scanner input = new Scanner(System.in); Time time = new Time(); System.out.println("Enter the time"); System.out.print("Hours: "); time.setHour(input.nextInt()); System.out.print("Minutes: "); time.setMinute(input.nextInt()); System.out.print("Seconds: "); time.setSecond(input.nextInt()); System.out.printf("Hour: %d Minute: %d Second: %d ", time.getHour(), time.getMinute(), time.getSecond()); System.out.printf("Universal time: %s Standard time: %s ", time.toUniversalString(), time.toString()); int choice = getMenuChoice(); while( choice != 5 ) { switch ( choice ) { case 1: // add 1 second time.tick(); break; case 2: // add 1 minute time.incrementMinute(); break; case 3: // and 1 hour time.incrementHour(); break; case 4: // add arbitrary seconds System.out.print("Enter seconds to tick: "); int ticks = input.nextInt(); /* Write a looping mechanism to increment the seconds */ /* by calling tick the correct number of times */ for( int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote