Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Write a description of class Time here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class Time
  9. {
  10.     private int hour; // 0 - 23
  11.     private int minute; // 0 - 59
  12.     private int second; // 0 - 59
  13.    
  14.     Time(){
  15.     }    
  16.    
  17.     // set a new time value using universal time; throw an
  18.     // exception if the hour, minute or second is invalid.
  19.     public void setTime (int h, int m, int s)
  20.     {
  21.         // validate hour, minute and second
  22.         if ((h >= 0 && h < 24) && ( m >= 0 && m < 60) && (s >= 0 && s < 60))
  23.         {
  24.             hour = h;
  25.             minute = m;
  26.             second = s;
  27.         } // end if
  28.         else
  29.         {
  30.             throw new IllegalArgumentException
  31.             ("hour, minute and/or second was out of range");
  32.         }
  33.     } // end method setTime
  34.    
  35.     // convert to String in universal-time format (HH:MM:SS)
  36.     public String toUniversalString()
  37.     {
  38.         return String.format ("%02d:%02d:%02d", hour, minute, second);
  39.     } // end method toUniversalString
  40.    
  41.     // convert to String in standard-time format (H:MM:SS AM or PM)
  42.     public String toString()
  43.     {
  44.         return String.format ("%d:%02d:%02d %s",
  45.             (( hour == 0 || hour == 12 ) ? 12 : hour % 12),
  46.             minute, second, (hour < 12 ? "AM" : "PM"));
  47.     } // end method toString
  48. }