Question
need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes
need help with this JAVA lab, the starting code for the lab is below.
directions:
The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of
the clock. This class also has a method that compares two Clock instances and
returns the one that is set to earlier in the day. The blahblahblah class has a
method to get the hours, minutes and meridian from the user. Then a main method
in that class creates two Clock instances: one set to the time of an appointment
(12:30 p.m.) and the other set to the time input by the user.
If the user time is before the appointment,
the program tells the user that they are not late; otherwise, it tells
them that they are late.Your tasks for this lab are:\
Modify the getUserHours method to throw a custom exception called InvalidHourException (i.e. you should make your own class that is a subclass
of the built-in Java Exception class) if the user enters an invalid value
, such asa value that is not an integer, a negative value, or a value greater than 12.
Modify the getUserMinutes method to throw a custom exception called
InvalidMinuteException (i.e. you should make your own class that is asubclass of the built-in Java Exception class) if the user enters an invalid
value.
modify the main method to catch these exceptions, display an appropriate
error message to the user, and end the programs execution.
Add an assertion to the Clock classs getEarlier method to formally check what is currently written in the comments
(i.e. that the clocks have the same time if they get to the last else clause)
output should look something like this but with more test cases:
What hour should the clock be set to? 13
You entered an invalid value for the hour What hour should the clock be set to? 5
What minute should the clock be set to? -2
You entered an invalid value for the minutes
What hour should the clock be set to? 11
What minute should the clock be set to? 15
Is it a.m. (a) or p.m. (p)? a
You're not late!
here is the starting code:
***blahblahblah**** driver class**
import java.util.Scanner;
public class blahblahblah {
static Scanner input = new Scanner(System.in); public static void main(String[] args) { Clock appointmentTime = new Clock(12, 30, "p.m."); Clock userTime = new Clock(getUserHours(), getUserMinutes(), getUserMeridian()); if (Clock.getEarlier(userTime, appointmentTime) == userTime) { System.out.println("You're not late!"); } else { System.out.println("You're late!"); } } public static int getUserHours() { System.out.print("What hour should the clock be set to? "); int hours = input.nextInt(); input.nextLine(); // consumes the trailing newline return hours; } public static int getUserMinutes() { System.out.print("What minute should the clock be set to? "); int hours = input.nextInt(); input.nextLine(); // consumes the trailing newline return hours; } public static String getUserMeridian() { System.out.print("Is it a.m. (a) or p.m. (p)? " ); String answer = input.nextLine(); if (answer.toLowerCase().startsWith("a")) { return "a.m."; } else { return "p.m."; } }
}
***end of blahblahblah driver class*****
********clock.java************
package blahblahblah;
public class Clock { private int hours; private int minutes; private String meridian; public Clock() { hours = 12; minutes = 0; meridian = "a.m."; } public Clock(int hours, int minutes, String meridian) { this.hours = hours; this.minutes = minutes; this.meridian = meridian; } @Override public String toString() { String time = hours + ":"; if (minutes < 10) { time += "0"; } time += minutes + " " + meridian; return time; } public static Clock getEarlier(Clock c1, Clock c2) { if (c1.meridian.equals("a.m.") && c2.meridian.equals("p.m.")) { return c1; } else if (c1.meridian.equals("p.m.") && c2.meridian.equals("a.m.")) { return c2; } else { // there is a special case if it is 12-something a.m. or p.m. on one // but not both of the clocks (i.e. 12:00 a.m. is before 1:00 a.m.) if (c1.hours == 12 && c2.hours != 12) { return c1; } else if (c2.hours == 12 && c1.hours != 12) { return c2; } else { if (c1.hours < c2.hours) { return c1; } else if (c2.hours < c1.hours) { return c2; } else { if (c1.minutes < c2.minutes) { return c1; } else if (c2.minutes < c1.minutes) { return c2; } else { // the clocks have the same time assert c1.toString().equals(c2.toString()): c1.toString() + " " + c2.toString(); // we will arbitrarily return the first one return c1; } } } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started