Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. **
  2.  * Time1.java
  3.  * Time1 class declaration maintains the time in 24-hour format.
  4.  *
  5.  * @Benito Danneswara
  6.  * @26 February 2017
  7.  */
  8.  
  9. public class Time1
  10. {
  11.     private int hour; // 0 - 23
  12.     private int minute; // 0 - 59
  13.     private int second; // 0 - 59
  14.    
  15.     // set a new time value using universal time; throw an
  16.     // exception if the hour, minute or second is invalid.
  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) && (s >= 0 && s < 60))
  21.         {
  22.             hour = h;
  23.             minute = m;
  24.             second = s;
  25.         } // end if
  26.         else
  27.         {
  28.             throw new IllegalArgumentException
  29.             ("hour, minute and/or second was out of range");
  30.         }
  31.     } // end method setTime
  32.    
  33.     // convert to String in universal-time format (HH:MM:SS)
  34.     public String toUniversalString()
  35.     {
  36.         return String.format ("%02d:%02d:%02d", hour, minute, second);
  37.     } // end method toUniversalString
  38.    
  39.     // convert to String in standard-time format (H:MM:SS AM or PM)
  40.     public String toString()
  41.     {
  42.         return String.format ("%d:%02d:%02d %s",
  43.             (( hour == 0 || hour == 12 ) ? 12 : hour % 12),
  44.             minute, second, (hour < 12 ? "AM" : "PM"));
  45.     } // end method toString
  46. } // end class Time1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement