
This lab involves working with generic and robust input and output. It has two parts:
Part A:
Develop a generic procedure that can be instantiated for an enumeration type. This procedure will simply display the values of the enumeration type, in the order in which they are defined, in a tabular form. The spec for this procedure will be
GENERIC TYPE Enum IS <>; PROCEDURE DisplayTable;
Each cell in the table should be the same width (number of characters); the row should be centered on the screen and its width should not exceed 60.
The number of rows and columns in the table will depend on the number of literals in the enumeration type, and on the number of characters in the widest literal. Luckily, Ada has some useful attribute functions to help with this. The number of literals in a type E is, of course, E'Pos(E'Last) + 1; the width of the widest literal is given by E'Width.
To display a row, write the proper number of literals into a string, then display the string. Ada.Text_IO.Enumeration_IO provides a procedure to help here:
PROCEDURE Put(To: OUT String;
Item: IN Enum;
Set: IN Type_Set := Default_Setting);
This procedure outputs the value of the parameter Item to the given string, using the length of the given string as the desired width; the string is right-padded with blanks. The string can be given as a slice, so you can control exactly which positions in the string will be occupied by each value.
Part B:
Modify the package programs51/robust_input so that the calling program can specify the prompt and error messages. Also, add to the robust input package a generic procedure Get that can be instantiated for an enumeration type. This procedure will use the procedure from Part A as part of its prompting. The new spec will look like
PACKAGE Robust_Input IS
------------------------------------------------------------------
--| Package for getting numeric input robustly.
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: February 1999
-----------------------------------------------------------------:
PROCEDURE Get (Item : OUT Integer;
MinVal : IN Integer;
MaxVal : IN Integer;
Prompt: IN String := "";
ErrorMessage: IN String := "");
-- Gets an integer value in the range MinVal..MaxVal from the terminal
-- Pre: MinVal and MaxVal are defined
-- Post: MinVal <= Item <= MaxVal
PROCEDURE Get (Item : OUT Float;
MinVal : IN Float;
MaxVal : IN Float;
Prompt: IN String := "";
ErrorMessage: IN String := "");
-- Gets a float value in the range MinVal..MaxVal from the terminal
-- Pre: MinVal and MaxVal are defined
-- Post: MinVal <= Item <= MaxVal
GENERIC
TYPE Enum IS <>;
PROCEDURE Get (Item : OUT Enum;
Prompt: IN String := "";
ErrorMessage: IN String := "");
-- Gets a value in the given enumeration type
-- Pre: None
-- Post: Item is in the given enumeration type
END Robust_Input;