Advertisement
Guest User

8.1

a guest
Feb 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. // Fig. 8.1: Time1.java
  2. // Time1 class declaration maintains the time in 24-hour format.
  3.  
  4. public class Time1
  5. {
  6.     private int hour;
  7.     private int minute;
  8.     private int second;
  9.  
  10.     public void setTime(int h, int m, int s)
  11.     {
  12.         if ( ( h >= 0 && h < 24 ) && ( m >= 0 && m < 60 ) &&
  13.         ( s >= 0 && s < 60 ) )
  14.         {
  15.             hour = h;
  16.             minute = m;
  17.             second = s;
  18.         }
  19.        
  20.         else
  21.             throw new IllegalArgumentException(
  22.             "hour, minute and/or second was out of range" );
  23.  
  24.     }
  25.  
  26.     public String toUniversalString()
  27.     {
  28.         return String.format( "%02d:%02d:%02d", hour, minute, second );
  29.     }
  30.    
  31.     public String toString()
  32.     {
  33.         return String.format( "%d:%02d:%02d %s",
  34.         ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
  35.         minute, second, ( hour < 12 ? "AM" : "PM" ) );
  36.     }    
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement