SE-1020 Software Development 2
Lab 7: Date Calculator Exception Handling

Outcomes

bulletimplement exception handling in an application to react to error conditions

Assignment

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.

Project Setup

bulletFirst create the Lab7 project. Download this lab7.zip file; then follow these steps to import it :
  1. With the Lab7 project selected in the Package Explorer select File -> Import...
  2. From the Import dialog select General -> Archive File and click Next
  3. Click the first Browse button to and select the lab7.zip file you downloaded
  4. When you have selected the lab7.zip file click Open and then Finish to complete the import. You will see a folder named src in your project; right-click this folder, and select Build Path/Use this source folder from the context menu. Once part of the build path, you'll see a single package: se1021.dateCalculator.YOURNAME.
bulletRename the se1021.dateCalculator.YOURNAME to match your MSOE username (see steps below).
  1. Select the package name in the package explorer
  2. Right-click and select Refactor -> Rename from the context menu
  3. Enter the new name and click OK

The lab7.zip file includes the following classes:

bulletDateCalculatorApp.java - A main class that is also the UI, which also contains an private inner EventHandler class.
bulletDateStringConverter.java - A "worker" class whose methods extract numerical day, month, and year values from a String representation of a date.
bulletDaysCalculator.java - A "worker" class that performs the actual calculations to compute the number of days between two specified dates.

Details

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

bullet
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 "/".
bullet
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,
bullet
values for YYYY must be a number from 0 to 9999
bullet
values for MM must be a number from 1 to 12
bullet
values for DD must be a number from 1 to 31
bullet
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,

bulletthe DateCalculatorApp class is responsible only for creating the UI and presenting information to the user
bullet
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)
bullet
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"):

bullet<nothing entered in either the Start Date field>: "You must enter a value for the Start Date"
bullet"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"
bullet00/04/1776 (or 13/04/1776) entered in the Start Date field: "Valid range for the months in the Start Date are 01-12"
bullet07/32/1776 (or 07/-2/1776) entered in the Start Date field: "Valid range for the days in the Start Date are 01-31"
bullet07/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).

Lab Submission (due date in Blackboard)

  1. Demonstrate your Date Calculator app to your instructor. If you can't do this by the end of class, you must demonstrate it sometime during Office Hours before the following lab. You will not receive a grade until you provide this demonstration. Demonstrations after the due date will be considered a late submission.
     
  2. Upload all three files through Blackboard (assignment "Lab 7: Date Calculator"). Be sure to keep copies of all your files, in case something gets lost.

Your grade will be based on the following criteria:

bulletMeeting 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.
 
bulletTechnical quality of your program. Technical quality consists of formatting, commenting, and following coding style guidelines.
 
bulletSpelling and grammar.
 
bulletTimeliness of submission as stated in the course policies.