Thursday 2/7 Week 4

Chapter 4

Student Notes

 

Objectives

Enumeration Types

Intro to packages

 

 

1.   Enumeration types

·        Definition:

·         

·        Useful for representing a fixed set of values that are

·         

·         

·        TYPE Expenses IS (entertainment, rent, utilities, food, clothing, automobile, insurance, miscellaneous);

 

ExpenseKind : Expenses;

·        Entertainment etc called

·        It is legal for the same enumeration literal to appear in more than one enumeration type. Ex

TYPE Traffic_Colors IS (Red, Yellow, Green);

TYPE Primary_Colors IS (Red, Yellow, Blue);

TYPE TV_Colors IS (Red, Blue, Green);

 

 

 

 

2.  Enumeration type Attributes and Operations

·        Consider the following Enumeration type:

TYPE Days IS (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); 

 

Today    : Days; -- current day of the week

Tomorrow : Days; -- day after Today

 

·        Enumeration types are useful because

·         

·        Each literal has a position in the list, starting with

·         

·         

·        First, gives the first, or lowest, value in the list

·         

·        Last, gives the last or greatest value in the list

·         

·        Pos, given the                           of an enumeration type, return the                      of that value in the list

·        Val, given the                         of an enumeration type, returns the                    of the enumeration type at that position

·        Pred, given a              of an enumeration type, returns the                    of that value

·        Succ, given a               of an enumeration type, returns the                   of that value

 

Given that Today := Friday; and Tomorrow := Saturday;

 

Days’First is

Days’Last is

Days’Pos(Tuesday) is  

Days’Val(1) is 

Days’Pos(Sunday) is

Days’Val(6) is

Days’Pred(Tomorrow) is

Days’Pred(Today) is

Days’Succ(Monday) is

Days’Succ(Today) is

 

·        Note that enumeration types are not cyclical, they do not “wrap around” and therefore some queries will generate a Constraint_Error

·         

·        Days’Pred(Monday); or Days’Succ(Sunday); or possibly

      Tomorrow := Days’Succ(Today);

 

3.  Input/Output with Enumeration Types

·        Enumeration types need I/O, but cannot use Ada.Text_IO because enumeration types are each a unique type

·         

·        There is a                                  in Ada which allows for enumeration I/O.

·         

·         

·        In order to use this for our type, an                     of the generic package must be created. This is called                             the package.

 

      PACKAGE Day_IO IS NEW Ada.Text_IO.Enumeration_IO(Enum => Days);

·        This gives us the ability to use Get and Put operations

·        Day_IO.Get(Item => Today);

·        Day_IO.Put(Item => Today);

·        Day_IO.Put(Item => Today, Width => 10);

·        Day_IO.Put(Item => Today, Width => 10, Set => Text_IO.Lower_Case);

·        Day_IO.Put(Item => Today, Width => 10, Set => Text_IO.Upper_Case);

·         

·        When using Get, you will raise a Data_Error is you try to get a value that is not of the enumeration type

·        Ex. colors.adb

 

4.   Intro to packages

·        We will be looking at a package specification for the Ada.Calendar package

·        The specification serves two roles:

·         

·         

·         

·        AND

·         

·         

·         

·        Some resources available in a package are type and subtypes, procedures, and functions

·        Ex. Ada.Calendar

·         PRIVATE types specify a type whose details