ADA Data Structures b Subtype b Enumeration type
ADA Data Structures b. Subtype b. Enumeration type
SUBTYPE Definition : A subset of the values associated with the original type. Example : SUBTYPE Natural IS Integer RANGE 0. . Integer’Last; SUBTYPE Positive IS Integer RANGE 0. . Integer’Last;
More Examples SUBTYPE Small. Int IS Integer RANGE -50. . 50; SUBTYPE Non. Neg. Float IS Float RANGE 0. 0. . Float’Last; SUBTYPE Cap. Letter IS Character RANGE ‘A’. . ’Z’; X, Y, Z : Small. Int; Next. Char : Cap. Letter; Hours_Worked : Non. Neg. Float;
Constraint Error SUBTYPE Small. Int IS Integer RANGE -50. . 50; X, Y, Z : Small. Int; X : = 26; Y : = 28; Z : = X + Y; Compilation Error ? At Run Time, Error will be raised. Why ?
Compatibility Rules for Types and Subtypes Do not allow to mix the types of operands Examples: v 1 : Integer; V 2 : Float; V 1+V 2 leads to compilation error
More Compatibility Example : SUBTYPE Small. Int IS Integer RANGE -50. . 50; V 1 : Integer; V 2 : Small. Int; V 1 + V 2 is Compatible.
More Compatibility Two values are compatible if they have the same type name or one value’s type is a subtype of the other value’s type.
Interesting Things X : Integer; Y : Natural; Think about X : = Y; and Y : = X;
No Compilation Error X : = Y; is always valid. But Y : = X; is not always valid at execution time. When ?
Ada Standard Library With Ada. Numerics; Example get the value of Pi as Ada. Numerics. Pi do not need to declare Pi as constant float. . .
Enumeration Type b Defined by a list of values taking the form of identifiers. b Useful in representing a fixed set of values that are not numerical such as days of the week, class year (freshman, sophomore, junior, senior) , etc. . .
Defining Enumeration Type Form TYPE enumeration-type ( identifier-list)
Examples TYPE Days IS (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Declaration Some_Day : Days; Today : Days; Valentine_Day : Days;
More Examples Assignment Statements Today : = Friday; Some_Day : = Monday;
Type Attributes and Operations Six important Attributes b First b Last b Pos - - position for a given value b Val - - value for a given position b Pred - - predecessor of a given value b Succ - - successor of a given value
Attribute Query Type’attribute-name(value) Example Days’First Days’Succ(Friday) Days’Pos(Sunday)
Input/Output for Enumeration Types b Within Ada. Text_IO, a generic package called “Enumeration_IO” must be used. b But it can not be used immediately. b Instances must be created
Format PACKAGE instance IS NEW Ada. Text_IO. Enumeration_IO (Enum => type);
Get Examples PACKAGE Day_IO IS NEW Ada. Text_IO. Enumeration ( Enum => Dats ); Day_IO. Get ( Item => Some_day); Day_IO. Get ( Item => Valentine_Day);
Put Examples Day_IO. Put ( Item => Some_Day, width => 5, Set => Lower_case);
Example Program with Enumeration Type with Ada. Text_IO; Procedure Colors Is Type English_Colors Is (white, black, red); Type French_Colors is (blanc, noir, rouge); Package Eng_Color_IO is New Ada. Text_IO. Enumeration_IO (Enum => English_Colors); Package Fr_Color_IO is New Ada. Text_IO. Enumeration_IO (Enum => French_Colors);
Eng_Color : English_Colors; Fr_Color : French_Colors; position : Natural; Begin -Ada. Text_IO. Put (Item =>" The first English color is "); Eng_Color_IO. Put(Item => English_Colors'First); Ada. Text_IO. New_Line; Ada. Text_IO. Put (Item => " Enter an English color : "); Eng_Color_IO. Get(Item => Eng_Color); position : = English_Colors'Pos(Eng_Color); Fr_Color : = French_Colors'Val(position); Ada. Text_IO. Put (Item => " The equavilent French Color is "); Fr_Color_IO. Put (Item => Fr_Color, Set =>Ada. Text_IO. Lower_Case); Ada. Text_IO. New_Line;
- Slides: 22