document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * Write a description of class Time2 here.
  4.  *
  5.  * @author Muhammad Naufaldillah
  6.  * @version 10 October 2020
  7.  */
  8. public class Time2
  9. {
  10.     private int hour; // 0 - 23
  11.     private int minute; // 0 - 59
  12.     private int second; // 0 - 59
  13.    
  14.     // Time2 no-argument constructor:
  15.     // initializes each instance variable to zero
  16.     public Time2()
  17.     {
  18.        this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
  19.     } // end Time2 no-argument constructor
  20.    
  21.     // Time2 constructor: hour supplied, minute and second defaulted to 0
  22.     public Time2( int h )
  23.     {
  24.        this( h, 0, 0 ); // invoke Time2 constructor with three arguments
  25.     } // end Time2 one-argument constructor
  26.    
  27.     // Time2 constructor: hour and minute supplied, second defaulted to 0
  28.     public Time2( int h, int m )
  29.     {
  30.        this( h, m, 0 ); // invoke Time2 constructor with three arguments
  31.     } // end Time2 one-argument constructor
  32.    
  33.     // Time2 constructor: hour, minute and second supplied
  34.     public Time2( int h, int m, int s )
  35.     {
  36.        setTime( h, m, s ); // invoke Time2 constructor with three arguments
  37.     } // end Time2 one-argument constructor
  38.    
  39.     // Time2 constructor: another Time2 object supplied
  40.     public Time2( Time2 time )
  41.     {
  42.        this(  time.getHour(), time.getMinute(), time.getSecond() ); // invoke Time2 constructor with three arguments
  43.     } // end Time2 one-argument constructor
  44.    
  45.     // Set Methods
  46.     // set a new time value using universal time;
  47.     // validate the data
  48.     public void setTime( int h, int m, int s )
  49.     {
  50.         setHour( h ); // set the hour
  51.         setMinute( m ); // set the minute
  52.         setSecond( s ); // set the second
  53.     } // end method setTime
  54.    
  55.     // validate and set hour
  56.     public void setHour( int h )
  57.     {
  58.         if ( h >= 0 && h < 24 )
  59.         {
  60.             hour = h;
  61.         }
  62.         else
  63.         {
  64.             throw new IllegalArgumentException( "hour must be 0-23" );
  65.         }
  66.     } // end method setHour
  67.    
  68.     // validate and set minute
  69.     public void setMinute( int m )
  70.     {
  71.         if ( m >= 0 && m < 60 )
  72.         {
  73.             minute = m;
  74.         }
  75.         else
  76.         {
  77.             throw new IllegalArgumentException( "minute must be 0-59" );
  78.         }
  79.     } // end method setMinute
  80.    
  81.     // validate and set second
  82.     public void setSecond( int s )
  83.     {
  84.         if ( s >= 0 && s < 60 )
  85.         {
  86.             second = ( ( s >= 0 && s < 60 )?s: 0 );
  87.         }
  88.         else
  89.         {
  90.             throw new IllegalArgumentException( "second must be 0-59" );
  91.         }
  92.     } // end method setSecond
  93.    
  94.     // Get Methods
  95.     // get hour value
  96.     public int getHour()
  97.     {
  98.        return hour;
  99.     } // end method getHour
  100.    
  101.     // get minute value
  102.     public int getMinute()
  103.     {
  104.        return minute;
  105.     } // end method getMinute
  106.    
  107.     // get second value
  108.     public int getSecond()
  109.     {
  110.        return second;
  111.     } // end method getSecond
  112.    
  113.     // convert to String in universal-time format (HH:MM:SS)
  114.     public String toUniversalString()
  115.     {
  116.         return String.format( "%02d:%02d:%02d", hour, minute, second );
  117.     } // end method toUniversalString
  118.    
  119.     // convert to String in standard-time format (H:MM:SS AM or PM)
  120.     public String toString()
  121.     {
  122.         return String.format( "%d:%02d:%02d %s",
  123.             ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
  124.             minute, second, ( hour < 12 ? "AM" : "PM" ) );
  125.     } // end method toString
  126. } // end class Time2
  127.  
');