-- All of the code samples from class are included in this file.

-- You can extract and copy each of them and try to compile and run them.

-- They illustrate the use of enumeration types.

-- They illustrate the use of the attributes of enumeration types

-- They illustrate the handling of exceptions

-- They illustrate the use of Ada’s random number generator

-- Enjoy!

 

procedure testEnumeration is   -- shows type declarations but has compile time errors

 

type car is (ford, chevy, lincoln, bmw);

type city is (rome, naples, bologna);

type die is (1,2,3,4,5,6);

type die is (one, two, three, four, five,six);

type city is (new york, paris);

type city is (old town, paris);

 

begin

null;

end testEnumeration;

 

procedure testEnumeration2 is  -- shows type declarations & instantiations

                               -- but is missing with clause

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

type city is (rome, naples, bologna);

package city_io is new ada.text_io.enumeration_io (city);

 

begin

null;

end testEnumeration2;

 

with ada.text_io;                     

procedure testEnumeration3 is    -- compiles & can be run but has not output

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

type city is (rome, naples, bologna);

package city_io is new ada.text_io.enumeration_io (city);

 

begin

null;

end testEnumeration3;

 

with ada.text_io;

procedure testEnumeration5 is       -- declares a variable of type car

                                    -- gets a value of type car from the user

                                    -- echoes it but no prompt and bombs if user

                                    -- enters an illegal value

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

myCar : car;

 

begin

  car_io.get( myCar);

  car_io.put (myCar);

end testEnumeration5;

 

with ada.text_io;

procedure testEnumeration6 is  -- attempt at handling data_error exception

                               -- fails because it doesn’t know where data_error

                               -- comes from

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

myCar : car;

 

begin

  car_io.get( myCar);

  car_io.put (myCar);

  exception

     when data_error => 

          ada.text_io.put_line (" illegal input encountered ");

end testEnumeration6;

 

 

with ada.text_io;

procedure testEnumeration7 is    -- indicates where data_error comes from but

                                 -- can’t find ada.IO_exceptions package

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

myCar : car;

 

begin

  car_io.get( myCar);

  car_io.put (myCar);

  exception

     when ada.Io_exceptions.data_error => 

           ada.text_io.put_line (" illegal input encountered ");

end testEnumeration7;

 

 

with ada.text_io;

with ada.IO_exceptions;

procedure testEnumeration8 is    -- this compiles, runs and catches a single error

                                 -- and quits OR gets and puts a valid entry

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

myCar : car;

 

begin

  car_io.get( myCar);

  car_io.put (myCar);

  exception

     when ada.Io_exceptions.data_error => 

           ada.text_io.put_line (" illegal input encountered ");

end testEnumeration8;

 

 

with ada.text_io;

   with ada.IO_exceptions;

    procedure testEnumeration9 is  -- this version loops until the user enters a

                                   -- valid car name

  

      type car is (ford, chevy, lincoln, bmw);

       package car_io is new ada.text_io.enumeration_io (enum => car);

  

      myCar : car;

  

   begin

      loop

         ada.text_io.put (" enter a car name ");

         begin   

            car_io.get( myCar);

            car_io.put (myCar);

            exit;

             exception 

               when ada.Io_exceptions.data_error => 

                  ada.text_io.put_line (" illegal input encountered ");

         end;          

      end loop; 

   end testEnumeration9 ;

 

--The next series of programs used the attibutes of an enumeration type

with ada.text_io;

with ada.integer_text_io;

procedure testEnumerationA is    -- this one demonstrates that the position of

                                 -- the first value in the enumeration type’s list

                                 -- is 0                                

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

type city is (rome, naples, bologna);

package city_io is new ada.text_io.enumeration_io (city);

position : integer;

 

begin

position := car'pos(ford);

ada.integer_text_io.put (position);

 

end testEnumerationA;

 

with ada.text_io;

with ada.integer_text_io;

procedure testEnumerationB is    -- this one shows the use of attributes ‘first and

                                 -- ‘last  and that a for loop can chain through

                                 -- the entire range

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

type city is (rome, naples, bologna);

package city_io is new ada.text_io.enumeration_io (city);

position : integer;

 

begin

position := car'pos(ford);

ada.integer_text_io.put (position);

ada.text_io.new_line;

for i in car'pos(car'first)..car'pos (car'last) loop

    ada.integer_text_io.put (i);

       ada.text_io.new_line;

end loop;  

end testEnumerationB;

 

-- this procedure also shows the use of the attribute ‘val

 

with ada.text_io;

with ada.integer_text_io;

procedure testEnumerationC is 

 

type car is (ford, chevy, lincoln, bmw);

package car_io is new ada.text_io.enumeration_io (enum => car);

 

type city is (rome, naples, bologna);

package city_io is new ada.text_io.enumeration_io (city);

position : integer;

myCar : car;

 

begin

position := car'pos(ford);

ada.integer_text_io.put (position);

for i in car'pos(car'first)..car'pos (car'last) loop

    ada.integer_text_io.put (i);

       ada.text_io.new_line;

end loop;  

myCar :=  car'val(1);

car_io.put (myCar);

 

end testEnumerationC;

 

 

-- this code shows the use of Ada’s random number generator,  an example of a function,

-- the declaration of a subtype and the instantiation of that type.

-- it will compile but produces a warning and at runtime will result in a constraint_error

 

with Ada.Numerics.Discrete_random;

with Ada.integer_text_io;

with Ada.text_io;

function getRandom return integer is   

 

  subtype value is integer range 0..502;

  package randomValue is new Ada.Numerics.Discrete_Random (value);

 

 

    g : randomValue.generator;

    v : value;

  begin

   randomValue.reset(G); 

   for i in 1..50 loop

       v := randomValue.Random(g);

       ada.integer_text_io.put (v);

      end loop;

      v := 750;

             return (v);

end getRandom;

 

-- This illustrates the user of named and positional parameters (see notes)

-- for fun, try changing the base

 

with ada.integer_text_io;

procedure testPut is

number : integer;

begin

  number := 23;

  Ada.integer_text_io.put (number, 10);

  Ada.integer_text_io.put (item => number, width => 10);

  Ada.integer_text_io.put (width => 10, item => number);

  Ada.integer_text_io.put (item => number, width => 10, base => 10);

  Ada.integer_text_io.put (number, 10, 10);

  Ada.integer_text_io.put (base =>10, item => number, width => 10);

end testPut;