Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

image text in transcribedimage text in transcribed

public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

// Time2 no-argument constructor: initializes each instance variable // to zero; ensures that Time2 objects start in a consistent state public Time2() { this( 0, 0, 0 ); // invoke Time2 constructor with three arguments } // end Time2 no-argument constructor

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

// Time2 constructor: hour and minute supplied, second defaulted to 0 public Time2( int h, int m ) { this( h, m, 0 ); // invoke Time2 constructor with three arguments } // end Time2 two-argument constructor

// Time2 constructor: hour, minute and second supplied public Time2( int h, int m, int s ) { setTime( h, m, s ); // invoke setTime to validate time } // end Time2 three-argument constructor

// Time2 constructor: another Time2 object supplied public Time2( Time2 time ) { // invoke Time2 constructor with three arguments this( time.getHour(), time.getMinute(), time.getSecond() ); } // end Time2 constructor with Time2 argument

// Set a new time value using universal time. Perform // validity checks on data. Set invalid values to zero. /* Write header for setTime. */ { /* Write code here that declares three boolean variables which are initialized to the return values of setHour, setMinute and setSecond. These lines of code should also set the three member variables. */

/* Return true if all three variables are true; otherwise, return false. */ }

// validate and set hour /* Write header for the setHour method. */ { /* Write code here that determines whether the hour is valid. If so, set the hour and return true. */

/* If the hour is not valid, set the hour to 0 and return false. */ }

// validate and set minute /* Write the header for the setMinute method. */ { /* Write code here that determines whether the minute is valid. If so, set the minute and return true. */

/* If the minute is not valid, set the minute to 0 and return false. */ }

// validate and set second /* Write the header for the setSecond method. */ { /* Write code here that determines whether the second is valid. If so, set the second and return true. */

/* If the second is not valid, set the second to 0 and return false. */ }

// 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 /* Problem 2: Write the header for the tick method. */ { /* Write code here that increments the seconds by one. if seconds run out of bounds, you should consider incremeanting the minutes. */ } // end method tick

// Increment the minute /* Problem 2: Write the header for the incrementMinute method. */ { /* Write code here that increments the minutes by one. if minutes run out of bounds, you should consider incremeanting the hours. */ ; } // end method incrementMinute

// Increment the hour /* Problem 2: Write the header for the incrementHour method. */ { /* Write code here that increments the hours by one. Since we don't keep date in this object, if the hours run out of bounds it's no big deal. */ } // 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()

import java.util.Scanner;

public class Time2Test { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); Time2 time = new Time2(); // the Time2 object

int choice = getMenuChoice(); while ( choice != 4 ) // For problem 2 you may want to add 4 valid choises and quit with 5 instead of 4 { switch ( choice ) { case 1: // set hour System.out.print( "Enter Hours: " ); int hours = input.nextInt(); /* Write code here that sets the hour. If the hour is invalid, display an error message. */ break; case 2: // set minute System.out.print( "Enter Minutes: " ); int minutes = input.nextInt(); /* Write code here that sets the minute. If the minute is invalid, display an error message. */

break; case 3: // set seconds System.out.print( "Enter Seconds: " ); int seconds = input.nextInt(); /* Write code here that sets the second. If the second is invalid, display an error message. */

break; /* Problem 2: Write code here that calls method tick.*/ } // 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. Set Hour" ); System.out.println( "2. Set Minute" ); System.out.println( "3. Set Second" ); System.out.println( "4. Exit" ); System.out.print( "Choice: " ); /* Problem 2: You can add appropriate messages for calling method tick (adding a second to the time object)*/ return input.nextInt(); } // end method getMenuChoice } // end class Time2Test

Problem 1 Write and modify the set methods in class Time2 provided in the template to return appropriate error values if an attempt is made to set one of the instance variables hour, minute or second of an object of class Time2 to an invalid value. [Hint: Use boolean return types on each method.] Write a program that tests these new set methods and outputs error messages when incorrect values are supplied. Sample Output 1. Set Hour 2 Set Minute 3. set Second 4 Exit Choice 1. Enter Hours: 10 Hour 10 Minute 0 Second 0 Universal time: 10:00:00 Standard time: 10:00:00 AM 1. Set Hour 2 Set Minute 3 Set Second 4 Exit Choice 2 Enter Minutes 10 Hour 10 Minute 10 Second 0 Universal time: 10: 10:00 Standard time 10: 10:00 AM 1. Set Hour 2 Set Minute 3. set second 4 Exit Choice 3 Enter Seconds 10 Hour 10 Minute: 10 Second 10 Universal time: 10:10:10 Standard time: 10 10:10 AM 1 Set Hour 2 Set Minute 3 Set Second 4 Exit Choice 3 Enter Seconds 99 Invalid seconds. Hour 10 Minute 10 Second 0 Universal time: 10:10:00 Standard time: 10 10:00 AM 1 Set Hour 2 Set Minute 3 Set Second 4 Exit Choice 4 Bye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago