Source of Time.java


  1: // Fig. H.1: Time.java
  2: // Time class declaration with set and get methods.
  3: package com.deitel.jhtp6.appenH; // place Time in a package
  4: 
  5: /**
  6:  * This class maintains the time in 24-hour format.
  7:  * @see java.lang.Object
  8:  * @author Deitel & Associates, Inc.
  9:  */
 10: public class Time
 11: {
 12:    private int hour;   // 0 - 23
 13:    private int minute; // 0 - 59
 14:    private int second; // 0 - 59
 15: 
 16:    /** 
 17:     * Time no-argument constructor initializes each instance variable 
 18:     * to zero. This ensures that Time objects start in a consistent 
 19:     * state. @throws Exception In the case of an invalid time
 20:     */
 21:    public Time() throws Exception
 22:    {
 23:       this( 0, 0, 0 ); // invoke Time constructor with three arguments
 24:    } // end no-argument Time constructor
 25: 
 26:    /** 
 27:     * Time constructor
 28:     * @param h the hour
 29:     * @throws Exception In the case of an invalid time
 30:     */
 31:    public Time( int h ) throws Exception
 32:    { 
 33:       this( h, 0, 0 ); // invoke Time constructor with three arguments
 34:    } // end one-argument Time constructor
 35: 
 36:    /** 
 37:     * Time constructor
 38:     * @param h the hour
 39:     * @param m the minute
 40:     * @throws Exception In the case of an invalid time
 41:     */
 42:    public Time( int h, int m ) throws Exception
 43:    { 
 44:       this( h, m, 0 ); // invoke Time constructor with three arguments
 45:    } // end two-argument Time constructor 
 46: 
 47:    /** 
 48:     * Time constructor
 49:     * @param h the hour
 50:     * @param m the minute
 51:     * @param s the second
 52:     * @throws Exception In the case of an invalid time
 53:     */
 54:    public Time( int h, int m, int s ) throws Exception
 55:    { 
 56:       setTime( h, m, s ); // invoke setTime to validate time
 57:    } // end three-argument Time constructor 
 58: 
 59:    /** 
 60:     *  Time constructor
 61:     *  @param time A Time object with which to initialize
 62:     *  @throws Exception In the case of an invalid time
 63:     */
 64:    public Time( Time time ) throws Exception
 65:    {
 66:       // invoke Time constructor with three arguments
 67:       this( time.getHour(), time.getMinute(), time.getSecond() );
 68:    } // end Time constructor with Time argument
 69: 
 70:    /**
 71:     *  Set a new time value using universal time. Perform 
 72:     *  validity checks on the data. Set invalid values to zero.
 73:     *  @param h the hour
 74:     *  @param m the minute 
 75:     *  @param s the second
 76:     *  @see com.deitel.jhtp6.appenH.Time#setHour
 77:     *  @see Time#setMinute
 78:     *  @see #setSecond
 79:     *  @throws Exception In the case of an invalid time
 80:     */
 81:    public void setTime( int h, int m, int s ) throws Exception
 82:    {
 83:       setHour( h );   // set the hour
 84:       setMinute( m ); // set the minute
 85:       setSecond( s ); // set the second
 86:    } // end method setTime
 87: 
 88:    /**
 89:     *  Sets the hour.
 90:     *  @param h the hour
 91:     *  @throws Exception In the case of an invalid time
 92:     */
 93:    public void setHour( int h ) throws Exception
 94:    { 
 95:       if ( h >= 0 && h < 24 ) 
 96:          hour = h;
 97:       else 
 98:          throw( new Exception() );
 99:    } // end method setHour
100: 
101:    /**
102:     *  Sets the minute.
103:     *  @param m the minute
104:     *  @throws Exception In the case of an invalid time
105:     */
106:    public void setMinute( int m ) throws Exception
107:    {
108:       if ( m >= 0 && m < 60 ) 
109:          minute = m;
110:       else 
111:          throw( new Exception() );
112:    } // end method setMinute
113: 
114:    /**
115:     *  Sets the second.
116:     *  @param s the second.
117:     *  @throws Exception In the case of an invalid time
118:     */
119:    public void setSecond( int s ) throws Exception
120:    { 
121:       if ( s >= 0 && s < 60 ) 
122:          second = s;
123:       else 
124:          throw( new Exception() );
125:    } // end method setSecond
126: 
127:    /**
128:     *  Gets the hour.
129:     *  @return an <code>integer</code> specifying the hour.
130:     */
131:    public int getHour() 
132:    { 
133:       return hour; 
134:    } // end method getHour
135: 
136:    /**
137:     *  Gets the minute.
138:     *  @return an <code>integer</code> specifying the minute.
139:     */
140:    public int getMinute() 
141:    { 
142:       return minute; 
143:    } // end method getMinute
144: 
145:    /**
146:     *  Gets the second.
147:     *  @return an <code>integer</code> specifying the second.
148:     */
149:    public int getSecond() 
150:    { 
151:       return second; 
152:    } // end method getSecond
153: 
154:    /**
155:     *  Convert to String in universal-time format
156:     *  @return a <code>String</code> representation 
157:     *  of the time in universal-time format
158:     */   
159:    public String toUniversalString()
160:    {
161:       return String.format( 
162:          "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
163:    } // end method toUniversalString
164: 
165:    /** 
166:     *  Convert to String in standard-time format
167:     *  @return a <code>String</code> representation 
168:     *  of the time in standard-time format
169:     */   
170:    public String toStandardString()
171:    {
172:       return String.format( "%d:%02d:%02d %s", 
173:          ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ),
174:          getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
175:    } // end method toStandardString
176: } // end class Time
177: 
178: /**************************************************************************
179:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
180:  * Pearson Education, Inc. All Rights Reserved.                           *
181:  *                                                                        *
182:  * DISCLAIMER: The authors and publisher of this book have used their     *
183:  * best efforts in preparing the book. These efforts include the          *
184:  * development, research, and testing of the theories and programs        *
185:  * to determine their effectiveness. The authors and publisher make       *
186:  * no warranty of any kind, expressed or implied, with regard to these    *
187:  * programs or to the documentation contained in these books. The authors *
188:  * and publisher shall not be liable in any event for incidental or       *
189:  * consequential damages in connection with, or arising out of, the       *
190:  * furnishing, performance, or use of these programs.                     *
191:  *************************************************************************/