The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Spring 1999
Lab Exercise #7
For labs meeting Feb. 25-26, 1999

This project will give you some practice with exceptions and exception loops, and with modifying packages. Everything you need for this project is in Chapter 6 and earlier chapters.

In this project, you will modify the DayWeek package, and the program DayTest, so that the date handling is robust ("bulletproof").

Part 1:

Modify the program DayTest so that the Robust_Input package is used to get the month, day, and year.

Part 2:

Currently, the function DayWeek.DayOfWeek returns a meaningless value if it receives a bad combination of inputs (e.g., April 31, or February 29 of a non-leap year). As it happens, Ada.Calendar provides a built-in way to check the validity of a date. The function Ada.Calendar.Time_Of has the specification

  FUNCTION Time_Of (Year : Year_Number;
                    Month : Month_Number;
                    Day : Day_Number;
                    Seconds : Day_Duration:=0.0) RETURN Time;

and returns a value of type Ada.Calendar.Time, if and only if the month/day/year combination forms a valid date. Otherwise, Time_Of raises the exception Ada.Calendar.Time_Error. Let's use this to check the validity of a date. First we define our own exception: change the specification of DayWeek, to include the line

Date_Error: EXCEPTION;

Next, modify the function DayOfWeek. Declare a variable of type Ada.Calendar.Time, for example

TestDate: Ada.Calendar.Time;

then call Ada.Calendar.Time_Of with the 3 parameters received by DayOfWeek.

Test.Date :=
  Ada.Calendar.Time_Of(Month => Month, Day => Day, Year => Year);

put an exception handler in the body of DayOfWeek that looks like

EXCEPTION
  WHEN Ada.Calendar.Time_Error =>
    RAISE Date_Error;

Part 3:

Now add to DayTest a handler for DayWeek.Date_Error. Also change the program so that the user is requested to input five valid dates.