Introduction

In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will be printed to the console if the entry is not a valid military time.

Think about how you would convert any military time 00:00 to 23:59 into conventional time. Also think about what would be valid military times. To be a valid time, the data must have a specific form. First, it should have exactly 5 characters. Next, only digits are allowed in the first two and last two positions, and that a colon is always used in the middle position. Next, we need to ensure that we never have over 23 hours or 59 minutes. This will require us to separate the substrings containing the hours and minutes. When converting from military time to conventional time, we only have to worry about times that have hours greater than 12, and we do not need to do anything with the minutes at all. To convert, we will need to subtract 12, and put it back together with the colon and the minutes, and indicate that it is PM. Keep in mind that 00:00 in military time is 12:00 AM (midnight) and 12:00 in military time is 12:00 PM (noon).

Task #1 Character and String Class Methods

1.Copy the files Time.java and TimeDemo.java into NetBeans or other IDE tools. Note, the java files may not compile before you finish the coding.

2.In the Time.java file, add conditions to the decision structure which validates the data. Conditions are needed that will

a. Check the length of the string
b. Check the position of the colon
c. Check that all other characters are digits

3.Add lines that will separate the string into two substrings containing hours and minutes. Convert these substrings to integers and save them into the instance variables.

4.In the TimeDemo class, add a condition to the loop that converts the user's answer to a capital letter prior to checking it.

5.Compile, debug, and run. Test out your program using the following valid input: 00:00, 12:00, 04:05, 10:15, 23:59, 00:35, and the following invalid input: 7:56, 15:78, 08:60, 24:00, 3e:33, 1:111.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.