Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**Objective: Write a class converts 24-hour time to 12-hour time First download the driver and put it in your project DO NOT ALTER THE DRIVER!

**Objective:

Write a class converts 24-hour time to 12-hour time

  • First download the driver and put it in your project
    • DO NOT ALTER THE DRIVER!
  • Write a class file called TimeFormatException that inherits from Exception
  • Create the following constructors
    • Default calls the parents constructor and says something like This is an incorrect time
    • One that has a string parameter that passes that message to the parents constructor
  • Write a class file called Clock24 that DOES NOT HAVE a main method
  • Some of the attributes of Clock24 are
    • Hours
    • Minutes
    • isAM whether or not it is AM or PM
  • Create the following Constructors
    • Default sets everything to default values
      • Hours = 0 we assume that the 24th hour is 0
      • Minutes = 0
      • isAM = true
    • One that has the parameters
      • Hours
      • Minutes
  • Accessors and Mutators for each variable
    • MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
  • Create the following Methods
    • setTime this method will take in an hours value and a minutes value and if those values are valid then they are converted to 12-hour time (Dont forget to change from AM to PM and visa versa), and finally are set to the classes instance variables hours and minutes. If they are not valid such as if they were negative, they throw a TimeFormatException.
    • setTime this overloaded has a single string parameter formatted as hours:minutes, so for instance 17:45. This string value is to be broken down into separate hours and minutes and then those values are checked just like the other set time
      • Hint: For the hours part, use a loop to look at each character in the string adding those characters to another string until it reaches the :. Once it does reach : parse it. Same thing for minutes but after the :, and it ends when it reaches the end of the string.
      • In the parsing process you need to handle the general Exception that comes when the user enters in something that is not a number. Use a try catch for this.
    • printTime prints out the 12-hour time formatted as hours:minutes time of day, so 1:45 PM

**Driver code:

import java.util.*;

public class Clock24Driver {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Clock24 clock = new Clock24();

Scanner keyboard = new Scanner(System.in);

boolean quit = false;

while(quit == false)

{

//Prompt the user

System.out.println("Enter a time in the 24-hour notation or enter \"Exit\" to quit");

//Gets the user input

String input = keyboard.nextLine();

if(input.equalsIgnoreCase("exit"))

{

System.out.println("Goodbye!");

break;

}

try

{

clock.setTime(input);

clock.printTime();

}

catch(TimeFormatException e)

{

System.out.println(e.getMessage());

}

}

}

}

**Example Dialog:

Enter a time in the 24-hour notation or enter "Exit" to quit

2:45

2:45 AM

Enter a time in the 24-hour notation or enter "Exit" to quit

14:45

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

-1:56

EXCEPTION!: The hour is incorrect

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

asdf;lasdf

EXCEPTION!: Wrong format

EXCEPTION!: The hour is incorrect

2:45 PM

Enter a time in the 24-hour notation or enter "Exit" to quit

Exit

Goodbye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Explain how the role of training is changing.

Answered: 1 week ago

Question

7. General Mills

Answered: 1 week ago