Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Read this carefully and make certain that your program implements all parts exactly as described. Code should be as somplified and east to understant as

(Read this carefully and make certain that your program implements all parts exactly as described. Code should be as somplified and east to understant as possible. Use SPLIT(:) function.)

Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55).

Here are the input errors (exceptions) that your program should detect and deal with:

Receive the input from the user (give an example of the format you are expecting the user to use when entering the time) and check for the following:

  1. missing one of parts (the hour or the minutes or the second is missing.
  2. hh, mm or ss is not numeric
  3. hh not between 0 and 23
  4. mm is not between 0 and 59
  5. ss is not between 0 and 59

Be specific when reporting an error. For example, The input 24:04:20 contains an invalid hour of 24.

If the user gives you invalid input, start the whole process again by asking the user for a valid time. Keep prompting the user until you get a valid time. A while (!valid) loop would work well here.

If the time is valid, then output a message in this format, 23:45:16 is 11:45:16 pm. That is, convert it to the am/pm format.

The program should be coded as a series of method calls (within the try block, inside the while loop):

  1. getInput( ) asks the user for the time. Uses the String split( ) method to break it into 3 parts. Checks that there are 3 parts and throws a MissingColonException if there arent. The method returns the String array.
  2. checkHour( ) receives the hour String part of the array, tries to convert it to a numeric value, then checks if the number is between 0 and 23. Throws NumberFormatException and HourException
  3. checkMinutes( ) similar to checkHour( )
  4. checkSeconds( ) similar to checkHour( )

Notes:

  1. Be sure that the output that you submit shows that your program has tested ALL the different types of errors that can occur.

  1. The messages output by the catch blocks should be very specific. For example, output 24 is an invalid hour, or 23:15 is missing one part, rather than simply invalid hour or invalid input. The first 2 messages allow the user to better understand what input caused the problem; the other messages leave the user guessing about what the source of the problem is.

  1. You do not need to write code for the NumberFormatException class; this already exists as part of Java.

  1. Youll need to write class definition files for the other exceptions described above.

  1. Because Dr. Java does not show what the input is, you should write out the input before you do anything else (so that your professor can see what you entered).

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

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

6. Testing equipment that will be used in instruction.

Answered: 1 week ago