Advertisement
mnaufaldillah

Time1 Tugas 1

Oct 11th, 2020
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Time1 here.
  4.  *
  5.  * @author Muhammad Naufaldillah
  6.  * @version 10 October 2020
  7.  */
  8. public class Time1
  9. {
  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 universal time; throw an
  15.     // exception if the hour, minute or second is invalid
  16.     public void setTime( int h, int m, int s )
  17.     {
  18.         // validate hour, minute and second
  19.         if ( ( h >= 0 && h < 24 ) && ( m >= 0 && m < 60 ) && ( h >= 0 && h < 24 ) )
  20.         {
  21.             hour = h;
  22.             minute = m;
  23.             second = s;
  24.         } // end if
  25.         else
  26.         {
  27.             throw new IllegalArgumentException(
  28.                 "hour, minute and/or second was out of range" );
  29.         }
  30.     } // end method setTime
  31.    
  32.     // convert to String 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
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement