Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Time2 class definition with methods tick, // incrementMinute and incrementHour. public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 //All constructors ensure that Time2 objects start in a consistent state // no-argument constructor 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 )
- P a g e | 3
- { 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 // Perform validity checks on data. Set invalid values to zero. /* Write header for setTime. */ { /* Write code here and use boolean variables */ /* Return true if all three variables are true; otherwise, return false. */ } // validate and set data /* all set methods determine whether the supplied data value is valid. If so, set the field and return true. Otherwise, set the field to 0 and return false. */ // validate and set hour /* Write header for the setHour method. */ { */ } // validate and set minute /* Write the header for the setMinute method. */ { } // validate and set second /* Write the header for the setSecond method. */ { } // Get Methods // get hour value public int getHour()
- P a g e | 4
- { 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 toStandardString } // end class Time2 /********************************************************************* (C) Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved ***********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement