Project 8

The George Washington University
School of Engineering and Applied Science
Department of Electrical Engineering and Computer Science
CSci 51 -- Spring 1997
Project #8
Due Date: start of class, April 14, 1997

This project returns to the problem of dealing with calendar dates, using material from Chapters 1-8.

First, compile and run the package Simple_Dates (Programs 8.3 and 8.4) and the test program Test_Simple_Dates.

Next, modify the test program to make direct reference to the fields of a date variable. Try, for example, declaring

D: Simple_Dates.Date;

then, in the program body, writing

D.Month := Feb;
D.Day   := 30;
D.Year  := 1997;

This is not a very robust dates package, because it allowed you to store a weird month/day/year combination as a date value. Your project is to make this package better.

(A) First modify the specification to make Date a PRIVATE type, as follows:

WITH Ada.Calendar;
PACKAGE Simple_Dates IS
 
  TYPE Months IS
    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
 
  TYPE Date IS PRIVATE;
 
  PROCEDURE Get(Item: OUT Date);
  -- Pre:  None
  -- Post: Reads a date in mmm dd yyyy form, returning it in Item
 
  PROCEDURE Put(Item: IN Date);
  -- Pre:  Item is defined
  -- Post: Displays a date in mmm dd yyyy form
 
  FUNCTION Today RETURN Date;
  -- Pre:  None
  -- Post: Returns today's date
PRIVATE
  TYPE Date IS RECORD
    Month: Months;
    Day:   Ada.Calendar.Day_Number;
    Year:  Ada.Calendar.Year_Number;
  END RECORD;
 
END Simple_Dates;

Re-compile the package and try your test program again. The compilation errors show you one of the advantages of PRIVATE types.

(B) Now change the date input/output operations as follows:

 
  PROCEDURE Get(Item: OUT Date);
  -- Pre:  None
  -- Post: Reads a date ROBUSTLY in mmm dd yyyy form, returning it in Item

  PROCEDURE Put(Item: IN Date);
  -- Pre: Item is defined
  -- Post: Displays a date in mmm dd yyyy form
 
  PROCEDURE Get(File: IN Ada.Text_IO.File_Type; Item: OUT Date);
  -- Pre:  None
  -- Post: Reads a date in mmm dd yyyy form from the given file,
  --   returning it in Item. It is assumed that the dates in the file
  --   are valid.
 
  PROCEDURE Put(File: IN Ada.Text_IO.File_Type; Item: IN Date);
  -- Pre:  Item is defined
  -- Post: Writes a date in mmm dd yyyy form to the given file
 

and test the package as fully as you can. Note that your robust Get operation will be very similar to the one you wrote in Project 6 work.