document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Time2 class declaration with overloaded constructors.
  3.  *
  4.  * @author Muthia Qurrota Akyun
  5.  * 9 Oktober 2020
  6.  */  
  7.    
  8.  public class Time2  
  9.  {  
  10.    private int hour;  
  11.    private int minute;  
  12.    private int second;  
  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.    }  
  20.      
  21.    //Time2 constructor: hour supplied, minute and second defaulted to 0  
  22.    public Time2 (int h)  
  23.    {  
  24.      this (h, 0, 0);  
  25.    }  
  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);  
  31.    }  
  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 setTime to validate time  
  37.    }  
  38.      
  39.    //Time2 constructor: another Time2 object supplied  
  40.    public Time2(Time2 time)  
  41.    {  
  42.      this (time.getHour(), time.getMinute(), time.getSecond());  
  43.    }  
  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);  
  51.      setMinute(m);  
  52.      setSecond(s);  
  53.    }  
  54.      
  55.    //validate and set hour  
  56.    public void setHour(int h)  
  57.    {  
  58.      if (h >= 0 && h< 24)  
  59.        hour = h;  
  60.      else  
  61.        throw new IllegalArgumentException ("hour must b 0-23");  
  62.    }  
  63.      
  64.    //validate and set minute  
  65.    public void setMinute (int m)  
  66.    {  
  67.      if (m >= 0 && m < 60)  
  68.        minute = m;  
  69.      else  
  70.        throw new IllegalArgumentException ("minute must be 0-59");  
  71.    }  
  72.      
  73.    //validate and set second  
  74.    public void setSecond (int s)  
  75.    {  
  76.      if (s >= 0 && s <60)  
  77.        second = ((s >= 0 && s< 60) ? s : 0);  
  78.      else  
  79.        throw new IllegalArgumentException ("second must be 0-59");  
  80.    }  
  81.      
  82.    //gets methods  
  83.    //get hour value  
  84.    public int getHour()  
  85.    {  
  86.      return hour;  
  87.    }  
  88.      
  89.    //get minute value  
  90.    public int getMinute()  
  91.    {  
  92.      return minute;  
  93.    }  
  94.      
  95.    //get second value  
  96.    public int getSecond()  
  97.    {  
  98.      return second;  
  99.    }  
  100.      
  101.    //convert to String in universal-time format (HH:MM:SS)  
  102.    public String toUniversalString()  
  103.    {  
  104.      return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());  
  105.    }  
  106.      
  107.    //convert to String in standard-time format (H:MM:SS AM or PM)  
  108.    public String toString()  
  109.    {  
  110.      return String.format("%d:%02d:%02d %s",  
  111.        ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),  
  112.         getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));  
  113.    }  
  114.  }  
');