Timo_

Time2

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