hsbsb

Time2

Sep 16th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Time2 here.
  4.  *
  5.  *
  6.  *
  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.     // Time2 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.     // Time2 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-argument constructor
  31.    
  32.     // Time2 constructor: hour, minute and second supplied
  33.     public Time2( int h, int m, int s )
  34.     {
  35.         setTime( h, m, s ); // invoke setTime to validate time
  36.     } // end Time2 three-argument constructor
  37.    
  38.     // Time2 constructor: another Time2 object supplied
  39.     public Time2( Time2 time )
  40.     {
  41.        // invoke Time2 three-argument constructor
  42.        this( time.getHour(), time.getMinute(), time.getSecond() );
  43.     } // end Time2 constructor with a Time2 object argument
  44.  
  45.     public void setTime( int h, int m, int s )
  46.  {
  47.      setHour( h ); // set the hour
  48.      setMinute( m ); // set the minute
  49.      setSecond( s ); // set the second
  50.  } // end method setTime
  51.  
  52.  // validate and set hour
  53.  public void setHour( int h )
  54.  {
  55.      if ( h >= 0 && h < 24 )
  56.      hour = h;
  57.      else
  58.      throw new IllegalArgumentException( "hour must be 0-23" );
  59.  } // end method setHour
  60.  
  61.  // validate and set minute
  62.  public void setMinute( int m )
  63.  {
  64.      if ( m >= 0 && m < 60 )
  65.      minute = m;
  66.      else
  67.      throw new IllegalArgumentException( "minute must be 0-59" );
  68.  } // end method setMinute
  69.  
  70.  // validate and set second
  71.  public void setSecond( int s )
  72.  {
  73.     if ( s >= 0 && s < 60 )
  74.     second = ( ( s >= 0 && s < 60 )?s: 0 );
  75.     else
  76.     throw new IllegalArgumentException( "second must be 0-59" );
  77.  } // end method setSecond
  78.  
  79.     // Get Methods
  80.     // get hour value
  81.     public int getHour()
  82.     {
  83.     return hour;
  84.     } // end method getHour
  85.     // get minute value
  86.     public int getMinute()
  87.     {
  88.     return minute;
  89.     } // end method getMinute
  90.     // get second value
  91.     public int getSecond()
  92.     {
  93.     return second;
  94.     } // end method getSecond
  95.     // convert to String in universal-time format (HH:MM:SS)
  96.     public String toUniversalString()
  97.     {
  98.     return String.format(
  99.     "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  100.     } // end method toUniversalString
  101.    
  102.     // convert to String in standard-time format (H:MM:SS AM or PM)
  103.     public String toString()
  104.         {
  105.             return String.format( "%d:%02d:%02d %s",
  106.             ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
  107.             getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  108.         } // end method toString
  109.     } // end class Time2
Advertisement
Add Comment
Please, Sign In to add comment