public class Time5 { private int hour; private int minute; private int second; public void setTime(int hour, int minute, int second) { if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) { throw new IllegalArgumentException("hour, minute and second was out of range"); } this.hour = hour; this.minute = minute; this.second = second; } public String toUniversalString() { return String.format("%02d:%02d:%02d", hour, minute, second); } public String toString() { return String.format("%d:%02d:%02d %s",((hour == 0 || hour == 12)? 12: hour % 12), minute, second, (hour < 12 ? "AM" : "PM")); } } import java.util.Scanner; public class Time5Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Time5 time = new Time5(); displayTime("After time object is created", time); System.out.println(); System.out.println("Enter the hour: "); int hh = scanner.nextInt(); System.out.println("Enter the minutes: "); int mm = scanner.nextInt(); System.out.println("Enter the seconds: "); int ss = scanner.nextInt(); System.out.println(); time.setTime(hh, mm, ss); displayTime("After calling setTime", time); System.out.println(); try { time.setTime(69, 69, 69); } catch (IllegalArgumentException e) { System.out.printf("Exception: %s%n%n", e.getMessage()); } displayTime("After calling setTime with invalid values", time); } private static void displayTime(String header, Time5 t) { System.out.printf("%s%nUniversal time: %s%nStandard time: %s%n", header, t.toUniversalString(), t.toString()); } }