glennluc

8.1

Mar 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. //Time1.java
  2. //Time1 class declaration maintains the time in 24-hour format.
  3.  
  4. public class Time1
  5. {
  6. private int hour;
  7. private int minute;
  8. private int second;
  9.  
  10. public void setTime(int h, int m, int s)
  11. {
  12. if ( (h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60)){
  13. hour = h;
  14. minute = m;
  15. second= s;
  16. }
  17. else
  18. throw new IllegalArgumentException(
  19. "hour, minute and/or second was out of range");
  20. }
  21.  
  22. public String toUniversalString(){
  23. return String.format("%02d:%02d:%02d", hour, minute, second);
  24. }
  25.  
  26. public String toString () {
  27. return String.format("%d:%02d:%02d %s",
  28. ((hour == 0 || hour == 12) ? 12 : hour % 12),
  29. minute, second, (hour < 12 ? "AM" : "PM"));
  30. }
  31. }
Add Comment
Please, Sign In to add comment