Advertisement
Guest User

Untitled

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