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