The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Fall 1996
Project #7
Due Date: start of class, Nov. 7, 1996

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 body of DayWeek. Use exception handling to check each value as it is entered, to be sure that the month is in the range Jan..Dec, the day is in the range of Ada.Calendar.Day_Number, and the year is in the range Ada.Calendar.Year_Number.

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;

Finally, add to your main program (your modified DayTest) a handler for DayWeek.Date_Error. Also change the program so that the user is requested to input five valid dates.