implement exception handling in an application to react to error conditions |
Recall that in a recent homework assignment, you designed the user interface for an application that computed the number of days between two dates:
In this assignment, you will add exception handling logic to a
partially-completed version of this application, where the majority of the
event-handling logic and computations have been completed for you.
First create the Lab7 project. Download this
lab7.zip
file; then follow these steps to import it :
| |
Rename the se1021.dateCalculator.YOURNAME
to match your MSOE username (see steps below).
|
The lab7.zip file includes the following classes:
DateCalculatorApp.java - A main class that is also the UI, which also contains an private inner EventHandler class. | |
DateStringConverter.java - A "worker" class whose methods extract numerical day, month, and year values from a String representation of a date. | |
DaysCalculator.java - A "worker" class that performs the actual calculations to compute the number of days between two specified dates. |
The Date Calculator application you get in lab7.zip is in fact, totally functional - once you create a project around the above files, you can run and test the application by entering various values for Start Date and End Date. However, note how fragile the application is - that is, you can easily make it crash by entering values that don't conform to the expected format. Besides crashing, you can make it produce negative values by entering an End Date that precedes the Start Date, and you can specify dates that are invalid, such as “13/32/1999” (month and day out of range). For our purposes, all these behaviors should be considered incorrect.
Your goal for this lab is to incorporate error checking, exception handling, error reporting, and error recovery into the application so that it responds “correctly” to badly-specified input. Specifically, the application must
detect and report errors in Start Date and End Date input syntax. The
accepted syntax is MM/DD/YYYY (e.g. “07/04/1776”; two digits for the
month and day, and 4 digits for the year), and invalid entries (e.g.
“7/4/1776”, “07041776”, “123”, “July 4 1776”, or “abcd”) must be
detected. Upon detection, an error message must be presented to the user
(using the displayErrorMessage() method of the DateCalculatorApp
class) that specifies whether the error was present in the Start Date or
End Date. If an error exists in both Dates, it is acceptable to report
only the error in the Start Date. The delimiter between digits must
be a slash "/".
| |||||||
detect and report errors in values specified for MM, DD, and YYYY. Upon
detection, an error message must be presented to the user (using
displayErrorMessage) that specifies whether the error was
present in the Month, Day, or Year specification. If errors exist in
more than one value, it is acceptable to report only the first error
detected. Simple error checking is all that is necessary; that is,
| |||||||
detect and report errors (using displayErrorMessage) in
Start/End Date specification for the case where the specified Start Date
is chronologically later than the End Date. Upon detection, and error
message must be presented to the user specifying that the End Date
preceded the Start Date. |
In the Java files you downloaded, no error handling has been implemented. Instead, find the comments containing the word “TODO” that indicate where you need to insert code that will incorporate proper error handling. Follow the detailed instructions contained within the comments; they tell you how you should Incorporate error detection, exception handling, and exception generation into the code to trap all possible types of errors in input and correctly report them so that the program will never crash or produce erroneous results.
Note that this application is structured in a pattern that separates responsibility for various functionality among three classes. That is,
the DateCalculatorApp class is responsible only for creating the UI and presenting information to the user | |
the DateStringConverter is responsible only for converting user input
from String format to numeric format (any exceptions must be propagated
back to the DateCalculatorApp class; no error messages are to be
displayed from within DateStringConverter) | |
the DaysCalculator class is responsible only for handling the
computation of the number of days between dates (any exceptions must be
propagated back to the DateCalculatorApp class; no error messages are to
be displayed from within DaysCalculator ) |
Be sure to thoroughly test your program. The following example dates (all incorrect) should produce the corresponding (or very similar) explicit error messages (it is NOT acceptable to produce vague error messages such as "an error occurred"):
<nothing entered in either the Start Date field>: "You must enter a value for the Start Date" | |
"nonsense" text (anything that does NOT conform to the format MM/DD/YYYY) entered in the Start Date field: "You must enter a Start Date using the format MM/DD/YYYY" | |
00/04/1776 (or 13/04/1776) entered in the Start Date field: "Valid range for the months in the Start Date are 01-12" | |
07/32/1776 (or 07/-2/1776) entered in the Start Date field: "Valid range for the days in the Start Date are 01-31" | |
07/04/-123 entered in the Start Date field: "The year in the Start Date must be greater than 0" |
If an entry is made containing multiple errors (for example, 13/32/-123), it is acceptable only to report the first error encountered.
The same type of message should be generated if any of the same conditions occur in the End Date. If both Start and End dates contain errors, it is acceptable to generate the error message only for the Start date.
Remember: Error messages must be explicit (telling the user exactly what is wrong), and no exceptions can be allowed to propagate to the Java Virtual Machine (you program must never crash).
Your grade will be based on the following criteria:
Meeting requirements; your program must compile and
run, and produce well-formed, explicit error messages. If you have trouble, you must come to see me. If you don't and you
submit a program that does not compile or produce detailed meaningful results will
receive a grade of no higher than 50, even after reworking your program. | |
Technical quality of your program. Technical quality consists of formatting, commenting, and
following coding style guidelines. | |
Spelling and grammar. | |
Timeliness of submission as stated in the course policies. |