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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

1.5.1 Mak<strong>in</strong>g the solution more general<br />

The calculation us<strong>in</strong>g the pocket calculator can be made more general by stor<strong>in</strong>g the price of the apples <strong>in</strong> the<br />

calculator's memory. The price of a specific amount of apples can then be calculated by retriev<strong>in</strong>g the stored price<br />

of the apples and multiply<strong>in</strong>g this retrieved amount by the quantity required. For example, to setup the price of<br />

apples <strong>in</strong> the calculator's memory and calculate the cost of 4.1 kilos of apples, the process is as follows:<br />

Pocket calculator Step Steps performed<br />

1 Enter the cost of a kilo of apples:<br />

C 1 . 2 0<br />

S M / *<br />

7 8 9<br />

4.92<br />

-<br />

2<br />

3<br />

Save this value to the calculator’s memory:<br />

S<br />

Retrieve the value from memory:<br />

M<br />

4 5 6 +<br />

1 2 3 C<br />

0 . =<br />

4<br />

5<br />

6<br />

Enter the operation to be performed:<br />

*<br />

Enter the number of kilos to be bought:<br />

4 . 1<br />

Enter calculate<br />

=<br />

To calculate the price for each customer’s order of apples, only steps 3—6 need be repeated. In essence, a<br />

generalized solution to the problem of f<strong>in</strong>d<strong>in</strong>g the price of any quantity of apples has been def<strong>in</strong>ed and<br />

implemented.<br />

1.6 Solv<strong>in</strong>g the problem us<strong>in</strong>g the <strong>Ada</strong> <strong>95</strong> language<br />

To solve the problem of calculat<strong>in</strong>g the cost of a quantity of apples us<strong>in</strong>g the programm<strong>in</strong>g language <strong>Ada</strong> <strong>95</strong>, a<br />

similar process to that used previously when us<strong>in</strong>g a pocket calculator is followed. This time, however, the<br />

<strong>in</strong>dividual steps are as follows:<br />

Note:<br />

Step Description<br />

1 Set the memory location Price_per_kilo to the cost per kilogram of the<br />

apples.<br />

2 Set the memory location Kilos_of_apples to the kilograms of apples<br />

required.<br />

3 Set the memory location Cost to the result of multiply<strong>in</strong>g the contents of<br />

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

Kilos_of_apples.<br />

4 Pr<strong>in</strong>t the contents of the memory location Cost.<br />

Although a shorter sequence of steps can be written to calculate 1.2 multiplied by 5.2 the above<br />

solution can easily be extended to allow the price of any number of kilograms of apples to be<br />

calculated.<br />

In <strong>Ada</strong> <strong>95</strong> like most programm<strong>in</strong>g languages when a memory location is required to store a value, it must first be<br />

declared. This is done for many reasons, some of these reasons are:<br />

• So that the type of items that are to be stored <strong>in</strong> this memory location can be specified. By<br />

specify<strong>in</strong>g the type of the item that can be stored the compiler can allocate the correct amount<br />

of memory for the item as well as check<strong>in</strong>g that a programmer does not accidentally try and<br />

store an <strong>in</strong>appropriate item <strong>in</strong>to the memory location.<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!