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