ghifariastaudi

Untitled

Oct 12th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. /**
  2.  * Time2.java
  3.  * Time2 class declaration with overloaded constructors.
  4.  * @Ghifari Astaudi Ukumullah
  5.  * @version (a version number or a date)
  6.  */
  7. public class Time2
  8. {
  9.     private int hour; // 0 - 23
  10.     private int minute; // 0 - 59
  11.     private int second; // 0 - 59
  12.    
  13.     // Time2 no argument constructor:
  14.     // initializes each instance variable to zero
  15.     public Time2()
  16.     {
  17.         this ( 0, 0, 0 ); // invoke Time2 constructor with three arguments
  18.     } // each Time2 no argument constructor
  19.    
  20.     // Time 2 constructor: hour supplied, minute and second defaulted to 0
  21.     public Time2 ( int h )
  22.     {
  23.         this ( h, 0, 0 ); // invoke Time2 constructor with three arguments
  24.     } // end Time2 one argument constructor
  25.    
  26.     // Time 2 constructor: hour and minute supplied, second defaulted to 0
  27.     public Time2 ( int h, int m )
  28.     {
  29.         this ( h, m, 0 ); // invoke Time2 constructor with three arguments
  30.     } // end Time2 two arguments constructor
  31.    
  32.     // Time 2 constructor: hour, minute, and second supplied
  33.     public Time2 ( int h, int m, int s )
  34.     {
  35.         setTime ( h, m, s ); // invoke Time2 constructor with three arguments
  36.     } // end Time2 three arguments constructor
  37.    
  38.     // Time2 constructor: another object supplied
  39.     public Time2 ( Time2 time )
  40.     {
  41.         // invoke Time2 three arguments contructor
  42.         this ( time.getHour(), time.getMinute(), time.getSecond() );
  43.     } // end Time2 constructor with a Time2 object arguments
  44.    
  45.     // Set Methods
  46.     // set a new time value using universal time;
  47.     // validate the data
  48.    
  49.     public void setTime ( int h, int m, int s )
  50.     {
  51.         setHour ( h ); // set hour
  52.         setMinute ( m ); // set minute
  53.         setSecond ( s ); // set second
  54.     } // end method setTime
  55.    
  56.     // validate and set hour
  57.     public void setHour ( int h )
  58.     {
  59.         if ( h >= 0 && h < 24 )
  60.             hour = h;
  61.         else
  62.             throw new IllegalArgumentException ( "hour must be 0 - 23");
  63.     } // end method setHour
  64.    
  65.     // validate and set minute
  66.     public void setMinute ( int m )
  67.     {
  68.         if ( m >= 0 && m < 60 )
  69.             minute = m;
  70.         else
  71.             throw new IllegalArgumentException ( "minute must be 0 - 59" );
  72.     } // end method setMinute
  73.    
  74.     // validate and set second
  75.     public void setSecond ( int s )
  76.     {
  77.         if ( s >= 0 && s < 60 )
  78.             second = ((s>=0 && s<60)? s : 0);
  79.         else
  80.             throw new IllegalArgumentException ( "second must be 0 - 59" );
  81.     } // end method setSecond
  82.      
  83.     // Get Methods
  84.     // get hour value
  85.     public int getHour ()
  86.     {
  87.         return hour;
  88.     } // end method getHour
  89.    
  90.     //get minute value
  91.     public int getMinute ()
  92.     {
  93.         return minute;
  94.     } // end method getMinute
  95.    
  96.     // get second value
  97.     public int getSecond()
  98.     {
  99.         return second;
  100.     } // end method getSecond
  101.    
  102.     // convert to String in universal-time format (HH:NMM:SS)
  103.     public String toUniversalString()
  104.     {
  105.         return String.format (
  106.             "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  107.     } // end method toUniversalString
  108.    
  109.     // convert to String in standard-time format (H:MM:SS) AM or PM
  110.     public String toString ()
  111.     {
  112.         return String.format ( "%d:%02d:%02d %s",
  113.             ( (getHour() == 0 || getHour() == 12 ) ? 12 : getHour()%12 ),
  114.             getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  115.     } // end method toString
  116. } // end class Time2
Advertisement
Add Comment
Please, Sign In to add comment