need help with this JAVA lab, the starting code for the lab is below. directions
ID: 3785596 • Letter: N
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 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 program’s execution.
•Add an assertion to the Clock class’s 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;
}
}
}
}
}
}
Explanation / Answer
Hi,
Please see below the modified classes andnew exception classes. Please comment for any queries/feedbacks.
Thanks,
Anita
InputMismatchException.java
import java.util.InputMismatchException;
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.");
try{
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!");
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
public static int getUserHours() throws InvalidHourException {
int hours=0;
try
{ System.out.print("What hour should the clock be set to? ");
hours = input.nextInt();
}
catch(InputMismatchException exception)
{
throw new InvalidHourException("You entered an invalid value for the hour!");
}
input.nextLine(); // consumes the trailing newline
if(hours<0 ||hours>12){
throw new InvalidHourException("You entered an invalid value for the hour!");
}
return hours;
}
public static int getUserMinutes() throws InvalidMinuteException {
int minutes=0;
try
{ System.out.print("What minute should the clock be set to? ");
minutes = input.nextInt();
}
catch(InputMismatchException exception)
{
throw new InvalidMinuteException("You entered an invalid value for the minutes!");
}
input.nextLine(); // consumes the trailing newline
if(minutes<0 ||minutes>60){
throw new InvalidMinuteException("You entered an invalid value for the minutes!");
}
return minutes;
}
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.";
}
}
}
Clock.java
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;
}
}
}
}
}
}
InvalidHourException.java
public class InvalidHourException extends Exception {
public InvalidHourException() {
// TODO Auto-generated constructor stub
}
public InvalidHourException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public InvalidHourException(Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public InvalidHourException(String arg0, Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public InvalidHourException(String arg0, Throwable arg1, boolean arg2,
boolean arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
}
InvalidMinuteException.java
public class InvalidMinuteException extends Exception {
public InvalidMinuteException() {
// TODO Auto-generated constructor stub
}
public InvalidMinuteException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public InvalidMinuteException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public InvalidMinuteException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public InvalidMinuteException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}
Sample output:
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!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.