trizehn

Untitled

Oct 14th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Time1 here.
  4.  *
  5.  * @author Daffa Tristan Firdaus
  6.  * @version 0.1 8 Oktober 2020
  7.  */
  8. public class Time1
  9. {
  10.     private int hour,minute,second;
  11.    
  12.     public void setTime(int h, int m, int s)
  13.     {
  14.         if ((h >= 0 && h< 24) && (m >= 0 && m < 60) && (s >=0 && s < 60))
  15.         {
  16.             hour = h;
  17.             minute = m;
  18.             second = s;
  19.         }
  20.         else throw new IllegalArgumentException("hour, minute and/or second was out of range");
  21.     }
  22.    
  23.     public String toUniversalString()
  24.     {
  25.         return String.format("%02d:%02d:%02d", hour, minute, second );
  26.     }
  27.    
  28.     public String toString()
  29.     {
  30.         return String.format("%d:%02d:%02d %s",
  31.         (( hour == 0 || hour == 12 ) ? 12 : hour % 12 ),
  32.         minute, second, ( hour < 12 ? "AM" : "PM" ));
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment