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

Language: Java Directions: 1. A skeleton copy of class ‘Time’ has been created,

ID: 3814312 • Letter: L

Question

Language: Java

Directions:

1. A skeleton copy of class ‘Time’ has been created, which is used by the class TimeTracker (Attached Below).
2. The template has comments, indicated by //, which documents the classes
3. 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)

Sample Output:

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
Choice:

Use the following template:

Explanation / Answer

I have modified the code. Please check.

CODE:

package temp;

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 )
{
/* 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 )
{
/* 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 */
public 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 */
   int sec = getSecond();
   if (sec == 59){
       setSecond(0);
       incrementMinute();
   }
   else{
       setSecond(sec+1);
   }
} // end method tick

// Increment the minute
public void incrementMinute()
/* Write header for method 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 */
   int min = getMinute();
   if(min == 59){
       setMinute(0);
       incrementHour();
   }
   else{
       setMinute(min+1);
   }
} // end method incrementMinute

// Increment the hour
/* Write header for method incrementHour. */
public void incrementHour()
{
/* Write code that increments the hour by one. */
/* Must call setHour() and handle rollover */
   int hr = getHour();
   if(hr == 23)
       setHour(0);
   else
       setHour(hr+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();
for(int i=0;i<ticks;i++){
   time.tick();
}
/* Write a looping mechanism to increment the seconds */
/* by calling tick the correct number of times */

break;
} // end switch

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() );

choice = getMenuChoice();
} // end while
} // end main

// prints a menu and returns a value corresponding to the menu choice
private static int getMenuChoice()
{
Scanner input = new Scanner( System.in );
  
System.out.println( "1. Add 1 second" );
System.out.println( "2. Add 1 Minute" );
System.out.println( "3. Add 1 Hour" );
System.out.println( "4. Add seconds" );
System.out.println( "5. Exit" );
System.out.print( "Choice: " );
  
  
return input.nextInt();
} // end method getMenuChoice
} // end class TimeTest

OUTPUT:
Enter the time
Hours: 12
Minutes: 59
Seconds: 59
Hour: 12 Minute: 59 Second: 59
Universal time: 12:59:59 Standard time: 12:59:59 PM
1. Add 1 second
2. Add 1 Minute
3. Add 1 Hour
4. Add seconds
5. Exit
Choice: 1
Hour: 13 Minute: 0 Second: 0
Universal time: 13:00:00 Standard time: 1:00:00 PM
1. Add 1 second
2. Add 1 Minute
3. Add 1 Hour
4. Add seconds
5. Exit
Choice: 4
Enter seconds to tick: 59
Hour: 13 Minute: 0 Second: 59
Universal time: 13:00:59 Standard time: 1:00:59 PM
1. Add 1 second
2. Add 1 Minute
3. Add 1 Hour
4. Add seconds
5. Exit
Choice: 1
Hour: 13 Minute: 1 Second: 0
Universal time: 13:01:00 Standard time: 1:01:00 PM
1. Add 1 second
2. Add 1 Minute
3. Add 1 Hour
4. Add seconds
5. Exit
Choice: 5