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 1<br />

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

This chapter looks at declarations and use of scalar data items <strong>in</strong> <strong>Ada</strong>. One of <strong>Ada</strong>'s key contributions to<br />

programm<strong>in</strong>g is the ability to declare data items that can only take a specific range of values. <strong>Ada</strong>’s strong<br />

typ<strong>in</strong>g ensures that many errors <strong>in</strong> a program will be detected at compile rather than run-time.<br />

4.1 Introduction<br />

So far, only objects of type Integer or Character have been <strong>in</strong>troduced. An Integer object stores a<br />

number as a precise amount with no decimal places. The exact range of values that can be stored is<br />

implementation def<strong>in</strong>ed. A user can f<strong>in</strong>d out this range by employ<strong>in</strong>g the attributes 'First and 'Last on the<br />

type Integer. In addition the attribute 'Bits returns the size <strong>in</strong> bits of an Integer object. The follow<strong>in</strong>g<br />

program pr<strong>in</strong>ts these attributes for an Integer type.<br />

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

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

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

Put("Smallest Integer ");Put( Integer'Image(Integer'First)); New_L<strong>in</strong>e;<br />

Put("Largest Integer ");Put( Integer'Image(Integer'Last)); New_L<strong>in</strong>e;<br />

Put("Integer (bits) ");Put( Integer'Image(Integer'Size )); New_L<strong>in</strong>e;<br />

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

Note:<br />

The attribute 'First is pronounced ‘tick first’.<br />

When compiled and run on two different mach<strong>in</strong>es, this would produce:<br />

Mach<strong>in</strong>e us<strong>in</strong>g a 16 bit word size<br />

Smallest <strong>in</strong>teger -32768<br />

Largest <strong>in</strong>teger 32767<br />

Integer (bits) 16<br />

Mach<strong>in</strong>e us<strong>in</strong>g a 32 bit word size<br />

Smallest <strong>in</strong>teger -2147483648<br />

Largest <strong>in</strong>teger 2147483647<br />

Integer (bits) 32<br />

4.2 The type Float<br />

An <strong>in</strong>stance of the type Integer holds numbers to an exact value. In the solution of some problems the numbers<br />

manipulated will not be an exact value. For example, a person's weight is 80.23 kilograms. The data type Float<br />

elaborates an object which can hold a number which has decimal places. Thus <strong>in</strong> a program a person's weight can<br />

be held <strong>in</strong> the object weight which is declared as follows:<br />

Weight : Float := 80.23;<br />

A Float is implemented as a float<strong>in</strong>g po<strong>in</strong>t number. A float<strong>in</strong>g po<strong>in</strong>t number holds a value to a specific<br />

number of decimal digits. This will <strong>in</strong> many cases be an approximation to the exact value which the programmer<br />

wishes to store. For example, a 1/3 will be held as 0.333 ... 33. The follow<strong>in</strong>g table shows how various numbers<br />

are effectively stored <strong>in</strong> float<strong>in</strong>g po<strong>in</strong>t form to 6 decimal places:<br />

Number Scientific notation Float<strong>in</strong>g po<strong>in</strong>t form<br />

80.23 0.8023 * 102 +802300 +02<br />

0.008023 0.8023 * 10-2 +802300 -02<br />

0.333333 0.333333 * 100 +333333 +00<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!