document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Fig. 8.5 : Deklarasi Time2 class.
  3.  *
  4.  * @author Naufaliando Yudo Kusumo
  5.  * @version Final
  6.  */
  7. public class Time2
  8. {
  9.     private int hour; // 0 - 23
  10.     private int minute; // 0 - 59
  11.     private int second; // 0 - 59
  12.    
  13.     public Time2()
  14.     {
  15.         this(0, 0, 0);
  16.     }
  17.    
  18.     public Time2(int h)
  19.     {
  20.         this(h, 0, 0);
  21.     }
  22.    
  23.     public Time2(int h, int m)
  24.     {
  25.         this(h, m, 0);
  26.     }
  27.    
  28.     public Time2(int h, int m, int s)
  29.     {
  30.         setTime(h, m, s);
  31.     }
  32.    
  33.     public Time2(Time2 time)
  34.     {
  35.         this(time.getHour(), time.getMinute(), time.getSecond());
  36.     }
  37.    
  38.     public void setTime(int h, int m, int s)
  39.     {
  40.         setHour(h); // set the hour
  41.         setMinute(m); // set the minute
  42.         setSecond(s); // set the second
  43.     }
  44.    
  45.     public void setHour(int h)
  46.     {
  47.         if (h>=0 && h<24)
  48.             hour = h;
  49.         else
  50.             throw new IllegalArgumentException("hour must be 0-23");
  51.     }
  52.    
  53.     public void setMinute(int m)
  54.     {
  55.         if (m>=0 && m<60)
  56.             minute = m;
  57.         else
  58.             throw new IllegalArgumentException("minute must be 0-59");
  59.     }
  60.    
  61.     public void setSecond(int s)
  62.     {
  63.         if (s>=0 && s<60)
  64.             second = ((s>=0 && s<60) ? s:0);
  65.         else
  66.             throw new IllegalArgumentException("second must be 0-59");
  67.     }
  68.    
  69.     public int getHour()
  70.     {
  71.         return hour;
  72.     }
  73.    
  74.     public int getMinute()
  75.     {
  76.         return minute;
  77.     }
  78.    
  79.     public int getSecond()
  80.     {
  81.         return second;
  82.     }
  83.    
  84.     public String toUniversalString()
  85.     {
  86.         return String.format(
  87.             "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  88.     }
  89.    
  90.     public String toString()
  91.     {
  92.         return String.format("%d:%02d:%02d %s",
  93.             ((getHour()==0 || getHour()==12) ? 12 : getHour()%12),
  94.             getMinute(), getSecond(), (getHour()<12 ? "AM" : "PM"));
  95.     }
  96. }
  97.  
');