Chapter 9 Lab More Classes and Objects Lab Objectives
Chapter 9 Lab More Classes and Objects Lab Objectives
Lab Objectives
Use methods of the Character class and String class to process text
Be able to use the String.split method and the StringBuilder class
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).
We will need to use a variety of Character class and String class methods to
validate the data and separate it in order to process it. We will also use a Character
class method to allow the user to continue the program if desired.
The String class’s split method will allow us to process the tokens in a text file in
order to decode a secret message. We will use the first letter of every 5th token read in
from a file to reveal the secret message.
/**
The toString method returns a conventional time.
@return A conventional time with am or pm.
*/
if (afternoon)
am_pm = "PM";
else
am_pm = "AM";
if (minutes < 10)
zero = "0";
/**
This program demonstrates the Time class.
*/