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.

Introduction to programm<strong>in</strong>g 5<br />

• The programmer does not accidentally store a value <strong>in</strong>to a memory location C0st when they<br />

meant Cost. The programmer accidentally typed zero (0) when they meant the letter (o).<br />

The sequence of steps written <strong>in</strong> pseudo English is transformed <strong>in</strong>to the follow<strong>in</strong>g <strong>in</strong>dividual <strong>Ada</strong> <strong>95</strong><br />

statements which, when obeyed by a computer, will display the cost of 5.2 kilograms of apples at £1.20 a<br />

kilogram.<br />

Step L<strong>in</strong>e <strong>Ada</strong> <strong>95</strong> statements<br />

1<br />

2<br />

3<br />

4<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

Price_per_kilo : Float;<br />

Kilos_of_apples : Float;<br />

Cost : Float;<br />

Price_per_kilo := 1.20;<br />

Kilos_of_apples := 5.2;<br />

Cost:= Price_per_kilo*Kilos_of_apples;<br />

Put( Cost );<br />

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

Note:<br />

Words <strong>in</strong> bold type are reserved words <strong>in</strong> the <strong>Ada</strong> <strong>95</strong> language and cannot be used for the name of a<br />

memory location.<br />

The name of the memory location conta<strong>in</strong>s the character _ to make the name more readable. Spaces <strong>in</strong><br />

the name of a memory location are not allowed.<br />

Each <strong>Ada</strong> <strong>95</strong> statement is term<strong>in</strong>ated with a ;.<br />

Multiplication is written as *.<br />

The <strong>in</strong>dividual l<strong>in</strong>es of code of the <strong>Ada</strong> <strong>95</strong> program are responsible for the follow<strong>in</strong>g actions:<br />

L<strong>in</strong>e<br />

1<br />

2—3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

Description<br />

Allocates a memory location called Price_per_kilo that is used to<br />

store the price per kilogram of apples. This memory location is of type<br />

Float and can hold any number that has decimal places.<br />

Allocates memory locations: Kilos_of_apples and Cost.<br />

Sets the contents of the memory location Price_per_kilo to 1.20. The<br />

:= can be read as 'is assigned the value'.<br />

Assign 5.2 to memory location Kilos_of_apples.<br />

Sets the contents of the memory location Cost to the contents of the<br />

memory location Price_per_kilo multiplied by the contents of the<br />

memory location Kilos_of_apples.<br />

Writes the contents of the memory location Cost onto the computer<br />

screen.<br />

Starts a new l<strong>in</strong>e on the computer screen.<br />

This solution is very similar to the solution us<strong>in</strong>g the pocket calculator, except that <strong>in</strong>dividually named<br />

memory locations are used to hold the stored values, and the calculation is expressed <strong>in</strong> a more human readable<br />

form.<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!