Question
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
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: missing one of parts (the hour or the minutes or the second is missing. hh, mm or ss is not numeric hh not between 0 and 23 mm is not between 0 and 59 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): 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. 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 checkMinutes( ) similar to checkHour( ) checkSeconds( ) similar to checkHour( )
Notes: Be sure that the output that you submit shows that your program has tested ALL the different types of errors that can occur.
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.
You do not need to write code for the NumberFormatException class; this already exists as part of Java.
Youll need to write class definition files for the other exceptions described above.
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
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