home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch20_1.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.2 KB  |  83 lines

  1.                        -- Chapter 20 - Programming exercise 1
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH20_1 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    type POWER is (GAS, STEAM, DIESEL, NONE);
  11.  
  12.    package Enum_IO is new Text_IO.Enumeration_IO(POWER);
  13.    use Enum_IO;
  14.  
  15.    type VEHICLE (Engine : POWER) is
  16.       record
  17.          Model_Year : INTEGER range 1888..1992;
  18.          Wheels     : INTEGER range 2..18;
  19.          case Engine is
  20.             when GAS    => Cylinders   : INTEGER range 1..16;
  21.             when STEAM  => Boiler_Size : INTEGER range 5..22;
  22.                            Coal_Burner : BOOLEAN;
  23.             when DIESEL => Fuel_Inject : BOOLEAN;
  24.             when NONE   => Speeds      : INTEGER range 1..15;
  25.          end case;
  26.       end record;
  27.  
  28.    Ford    : VEHICLE(GAS);
  29.    Truck   : VEHICLE(DIESEL);
  30.    Schwinn : VEHICLE(NONE);
  31.    Stanley : VEHICLE(STEAM);
  32.  
  33. begin
  34.  
  35.    Ford.Model_Year := 1956;       -- Component assignment
  36.    Ford.Wheels := 4;
  37.    Ford.Cylinders := 8;
  38.  
  39.    Ford := (GAS, 1956, 4, 8);     -- Positional aggregate assignment
  40.  
  41.                                   -- Named aggregate assignment
  42.    Ford := (Model_Year => 1956, Cylinders => 8, Engine => GAS,
  43.                                                       Wheels => 4);
  44.  
  45.    Stanley.Model_Year := 1908;
  46.    Stanley.Wheels := 4;
  47.    Stanley.Boiler_Size := 21;
  48.    Stanley.Coal_Burner := FALSE;
  49.                                   -- Mixed aggregate assignment
  50.    Stanley := (STEAM, 1908, 4, Coal_Burner => FALSE,
  51.                                                 Boiler_Size => 21);
  52.  
  53.    Schwinn.Speeds := 10;
  54.    Schwinn.Wheels := 2;
  55.    Schwinn.Model_Year := 1985;
  56.  
  57.    Truck.Model_Year := 1966;
  58.    Truck.Wheels := 18;
  59.    Truck.Fuel_Inject := TRUE;
  60.  
  61.    Put("Stanley engine type is ");
  62.    Put(Stanley.Engine);
  63.    Put(", and has");
  64.    Put(Stanley.Wheels,3);
  65.    Put_Line(" wheels.");
  66.  
  67.    Put("The truck is a");
  68.    Put(Truck.Model_Year,5);
  69.    Put(" and has");
  70.    Put(Truck.Wheels,3);
  71.    Put_Line(" wheels.");
  72.  
  73. end CH20_1;
  74.  
  75.  
  76.  
  77.  
  78. -- Result of Execution
  79.  
  80. -- Stanley engine type is STEAM, and has  4 wheels.
  81. -- The truck is a 1966 and has 18 wheels.
  82.  
  83.