Source of Time2.java


  1: // Fig. 8.5: Time2.java
  2: // Time2 class declaration with overloaded constructors.  
  3: 
  4: public class Time2
  5: {
  6:    private int hour;   // 0 - 23
  7:    private int minute; // 0 - 59
  8:    private int second; // 0 - 59
  9: 
 10:    // Time2 no-argument constructor: initializes each instance variable 
 11:    // to zero; ensures that Time2 objects start in a consistent state
 12:    public Time2()
 13:    {
 14:       this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
 15:    } // end Time2 no-argument constructor
 16: 
 17:    // Time2 constructor: hour supplied, minute and second defaulted to 0
 18:    public Time2( int h ) 
 19:    { 
 20:       this( h, 0, 0 ); // invoke Time2 constructor with three arguments
 21:    } // end Time2 one-argument constructor
 22: 
 23:    // Time2 constructor: hour and minute supplied, second defaulted to 0
 24:    public Time2( int h, int m ) 
 25:    { 
 26:       this( h, m, 0 ); // invoke Time2 constructor with three arguments
 27:    } // end Time2 two-argument constructor 
 28: 
 29:    // Time2 constructor: hour, minute and second supplied
 30:    public Time2( int h, int m, int s ) 
 31:    { 
 32:       setTime( h, m, s ); // invoke setTime to validate time
 33:    } // end Time2 three-argument constructor 
 34: 
 35:    // Time2 constructor: another Time2 object supplied
 36:    public Time2( Time2 time )
 37:    {
 38:       // invoke Time2 three-argument constructor
 39:       this( time.getHour(), time.getMinute(), time.getSecond() );
 40:    } // end Time2 constructor with a Time2 object argument
 41: 
 42:    // Set Methods
 43:    // set a new time value using universal time; ensure that 
 44:    // the data remains consistent by setting invalid values to zero
 45:    public void setTime( int h, int m, int s )
 46:    {
 47:       setHour( h );   // set the hour
 48:       setMinute( m ); // set the minute
 49:       setSecond( s ); // set the second
 50:    } // end method setTime
 51: 
 52:    // validate and set hour 
 53:    public void setHour( int h ) 
 54:    { 
 55:       hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); 
 56:    } // end method setHour
 57: 
 58:    // validate and set minute 
 59:    public void setMinute( int m ) 
 60:    { 
 61:       minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); 
 62:    } // end method setMinute
 63: 
 64:    // validate and set second 
 65:    public void setSecond( int s ) 
 66:    { 
 67:       second = ( ( s >= 0 && s < 60 ) ? s : 0 ); 
 68:    } // end method setSecond
 69: 
 70:    // Get Methods
 71:    // get hour value
 72:    public int getHour() 
 73:    { 
 74:       return hour; 
 75:    } // end method getHour
 76: 
 77:    // get minute value
 78:    public int getMinute() 
 79:    { 
 80:       return minute; 
 81:    } // end method getMinute
 82: 
 83:    // get second value
 84:    public int getSecond() 
 85:    { 
 86:       return second; 
 87:    } // end method getSecond
 88: 
 89:    // convert to String in universal-time format (HH:MM:SS)
 90:    public String toUniversalString()
 91:    {
 92:       return String.format( 
 93:          "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
 94:    } // end method toUniversalString
 95: 
 96:    // convert to String in standard-time format (H:MM:SS AM or PM)
 97:    public String toString()
 98:    {
 99:       return String.format( "%d:%02d:%02d %s", 
100:          ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
101:          getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
102:    } // end method toString
103: } // end class Time2
104: 
105: /**************************************************************************
106:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
107:  * Pearson Education, Inc. All Rights Reserved.                           *
108:  *                                                                        *
109:  * DISCLAIMER: The authors and publisher of this book have used their     *
110:  * best efforts in preparing the book. These efforts include the          *
111:  * development, research, and testing of the theories and programs        *
112:  * to determine their effectiveness. The authors and publisher make       *
113:  * no warranty of any kind, expressed or implied, with regard to these    *
114:  * programs or to the documentation contained in these books. The authors *
115:  * and publisher shall not be liable in any event for incidental or       *
116:  * consequential damages in connection with, or arising out of, the       *
117:  * furnishing, performance, or use of these programs.                     *
118:  *************************************************************************/