Question

need help with this JAVA lab, the starting code for the lab is below. directions: The...

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;
}
}
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!

Add a comment
Know the answer?
Add Answer to:
need help with this JAVA lab, the starting code for the lab is below. directions: The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • By editing the code below to include composition, enums, toString; must do the following: Prompt the...

    By editing the code below to include composition, enums, toString; must do the following: Prompt the user to enter their birth date and hire date (see Fig. 8.7, 8.8 and 8.9 examples) in addition to the previous user input Create a new class that validates the dates that are input (can copy date class from the book) Incorporate composition into your class with these dates Use enums to identify the employee status as fulltime (40 or more hours worked for...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT