document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // Fig 8.1: Time1.java
  2. // Time1 class declaration maintains the time in 24-hours format.
  3.  
  4. public class Time1
  5. {
  6.     private int hour; // 0 -23
  7.     private int minute; // 0 - 59
  8.     private int second; // 0 - 59
  9.    
  10.     //set a new time value using universal time; throw an
  11.     // exception if the hour, minute or second is invalid
  12.     public void setTime(int h, int m, int s)
  13.     {
  14.         //validate hour, minute and second
  15.         if ( ( h >= 0 && h < 24) && ( m >= 0 && m < 60 ) &&
  16.             ( s >= 0 && s < 60 ) )
  17.         {
  18.             hour = h;
  19.             minute = m;
  20.             second = s;
  21.         } // end if
  22.         else
  23.             throw new IllegalArgumentException(
  24.                 "hour, minute and/or second was out of range");
  25.     } // end method setTime
  26.    
  27.     //convert to String in universal-time format (HH:MM:SS)
  28.     public String toUniversalString()
  29.     {
  30.         return String.format( "%02d:%02d:%02d", hour, minute, second );
  31.     } // end method toUniversalString
  32.    
  33.     // convert to String in standard-time format (GH:MM:SS AM or PM)
  34.     public String toString()
  35.     {
  36.         return String.format( "%d:%02d:%02d %s",
  37.             ( (hour ==0 || hour == 12 ) ? 12 : hour % 12 ),
  38.             minute, second, ( hour < 12 ? "AM:" : "PM" ) );
  39.     } // end method toString
  40. } // end class Time1
');