18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Ada</strong> <strong>in</strong>troduction: Part 2 41<br />

4.4 Modified countdown program<br />

The countdown program shown earlier <strong>in</strong> Section 3.4 can be rewritten, restrict<strong>in</strong>g count to the values 1 to 10 as<br />

follows:<br />

with <strong>Ada</strong>.Text_Io;<br />

use <strong>Ada</strong>.Text_Io;<br />

procedure Ma<strong>in</strong> is<br />

type Count_Range is range 0 .. 10;<br />

Count : Count_Range := 10; --Declaration of count<br />

beg<strong>in</strong><br />

for Count <strong>in</strong> reverse Count_Range loop<br />

if Count = 3 then --If 3 pr<strong>in</strong>t Ignition<br />

Put("Ignition"); New_L<strong>in</strong>e;<br />

end if;<br />

Put( Count_Range'Image( Count ) ); --Pr<strong>in</strong>t current count<br />

New_L<strong>in</strong>e;<br />

Delay 1.0;<br />

-- Wait 1 second<br />

end loop;<br />

Put("Blast off"); New_L<strong>in</strong>e; --Pr<strong>in</strong>t Blast off<br />

end Ma<strong>in</strong>;<br />

Note: Even though count is of type Count_range, it can be compared with the <strong>in</strong>teger constant 3.<br />

The use of the type Count_Range <strong>in</strong> the loop statement. This conf<strong>in</strong>es the loop to the range of<br />

values that an <strong>in</strong>stance of Count_Range can take.<br />

The use of<br />

Count_Range'Image( Count )<br />

to deliver a character representation of the contents of count. Remember count is of type<br />

Count_Range.<br />

4.5 Input and output <strong>in</strong> <strong>Ada</strong><br />

One of the obstacles <strong>in</strong> writ<strong>in</strong>g programs <strong>in</strong> <strong>Ada</strong> is the complexity <strong>in</strong>volved <strong>in</strong> outputt<strong>in</strong>g <strong>in</strong>teger and float<strong>in</strong>g<br />

po<strong>in</strong>t numbers. To simplify this process <strong>Ada</strong> <strong>95</strong> provides the follow<strong>in</strong>g packages:<br />

• <strong>Ada</strong>.Integer_Text_Io for <strong>in</strong>put and output of <strong>in</strong>teger numbers.<br />

• <strong>Ada</strong>.Float_Text_Io for <strong>in</strong>put and output of float<strong>in</strong>g po<strong>in</strong>t numbers.<br />

• <strong>Ada</strong>.Text_Io for <strong>in</strong>put and output of characters and str<strong>in</strong>gs.<br />

Chapter 18 describes how specific packages <strong>in</strong> <strong>Ada</strong>.Text_Io are <strong>in</strong>stantiated to output <strong>in</strong>stances of other <strong>in</strong>teger<br />

and float<strong>in</strong>g po<strong>in</strong>t types.<br />

4.6 The package <strong>Ada</strong>.Float_Text_Io<br />

The package <strong>Ada</strong>.Float_Text_Io is used to <strong>in</strong>put and output float<strong>in</strong>g po<strong>in</strong>t numbers.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!