Java Reference
In-Depth Information
The first statement retrieves the selected text for the hour choice, parses the value to an int, and assigns it to
the variable shipHour. Because the Calendar object needs an hour value from 0 to 23, we need to calculate this by
checking if AM or PM was selected. The if statements retrieve the aMPMCh value and checks the hour value. If PM
was selected and the hour value is less than 12, 12 is added to the hour value. (E.g., if 12 && PM, then 12 is correct.
If 1 && PM, then 13 is correct.) Similarly, if the user specified 12 and AM, the correct hour value for the calendar
object is 0.
Next, the Calendar object (cal) is set to the values specified in the other choice fields and the calculated shipHour
value. Notice that for each choice, the selected value must be parsed to int . Also, the month value must be decreased
by one. (Because cal uses 0 to represent January, 1 to represent February, etc., we need to change a user specified 1 to
a 0, a 2 to a 1, etc.)
We then use cal to get a Date object (for the date and time specified) and assign it to the Date variable.
The last statement assigns the values from the text fields and the formatted date and time values to ship's
properties.
25.
Change the actionPerformed method to do the following:
Execute the setShipmentProperties method
Check the error flag
If (error flag is true )
Set error flag to false
Else
Create a ShipmentFrame object (passing ship)
26.
Save the EnterShipInfo source code.
Results of the Review Exercise
Here are the results:
1.
A new package called c7 in the ReviewEx project.
2.
Six newly coded classes in c7 (of 21 total classes) that are subclasses of Choice and that
display the current date and time.
3.
EnterShipInfo has:
A new method called setShipmentProperties that uses the new classes
Several new private class variables to work with dates and times
A new label to display messages
Check that the Exercise Was Done Correctly
Finally, let's check everything went as planned:
1.
Run TNT and ensure that the EnterShipInfo frame can be accessed.
2.
Verify that EnterShipInfo initially displays the current date and time in the choices.
3.
Verify that EnterShipInfo ensures that information is entered in the text fields and if not,
displays an error message in the new label.
4.
Enter several different dates and times and verify that ShipmentFrame displays the
information in the correct format.
Search WWH ::




Custom Search