Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Download: http://solutionzip.com/downloads/time2-java/
- Ex 8.7 (To enhance class Time2)
- Modify class Time2 of Fig. 8.5 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute by one and method incrementHour to increment the hour by one. The Time2 object should always remain in a consistent state. Write a program that test the trick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:
- a) incrementing into the next minute,
- b) incrementing into the next hour and
- c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
- New Java programming code logic is required to be added into this exercise Ex 8.7. You may copy an existing Java program example like Fig 8.5 below to start your work:
- Sample Java Code can be used for your program from Fig 8.5
- // Fig. 8.5: Time2.java
- // Time2 class declaration with overloaded constructors.
- 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 three-argument constructor
- this( time.getHour(), time.getMinute(), time.getSecond() );
- } // end Time2 constructor with a Time2 object argument
- // Set Methods
- // set a new time value using universal time; ensure that
- // the data remains consistent by setting 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
- // 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 toString
- } // end class Time2
- Modify Java logic in Fig 8.5 to perform:
- 1. Increase second and increase minute by 1 when second reaches 60 (actually it will be set to 0) in Fig 8.5
- public void tick()
- {
- setSecond( getSecond() + 1 );
- if ( getSecond() == 0 )
- incrementMinute();
- } // end method tick
- 2. Increase minute and increase hour by 1 when minute reaches 60 (actually it will be set to 0) in Fig 8.5. The logic is similar to (1)
- 3. Increase hour and set hour to 0 when hour reaches 24 (actually it will be set to 0) in Fig 8.5. The logic is similar to (1)
- 4. In test.java to add the logic below to test new tick method:
- setTime(23, 58, 55);
- input = 1;
- while (input == 1) {
- display time by toUniversalString();
- tick();
- display time by toUniversalString();
- prompt (1 to tick or 0 to end);
- read input;
- }
- Download: http://solutionzip.com/downloads/time2-java/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement