Advertisement
lamaulfarid

Time1

Oct 14th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. /**
  2.  * Write a description of class Time1 here.
  3.  * Time1 class declaration maintains the time in 24-hour format
  4.  * @author Ahmad Lamaul Farid
  5.  * @version 08 Oktober 2020
  6.  */
  7. public class Time1
  8. {
  9.     // instance variables - replace the example below with your own
  10.     private int hour;
  11.     private int minute;
  12.     private int second;
  13.    
  14.  
  15.     /**
  16.      * Constructor for objects of class Time1
  17.      */
  18.     public void setTime( int h, int m, int s)
  19.     {
  20.         // initialise instance variables
  21.         if( ( h >= 0 && h < 24 ) && ( m >= 0 && m <60 ) && ( s >= 0 && s < 60 ) )
  22.         {
  23.             hour = h;
  24.             minute = m;
  25.             second = s;
  26.         }
  27.         else
  28.         throw new IllegalArgumentException(
  29.             "hour, minute and/or second was out of range" );
  30.     }
  31.  
  32.     public String toUniversalString()
  33.     {
  34.         return String.format( "%02d:%02d:%02d", hour, minute, second );
  35.     }
  36.    
  37.     public String toString()
  38.     {
  39.         return String.format( "%d:%02d:%02d %s",
  40.             ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
  41.             minute, second, ( hour < 12 ? "AM" : "PM" ) );
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement