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.

172 Child libraries<br />

12.2 A rational arithmetic package<br />

If precise arithmetic with rational numbers is required the <strong>Ada</strong> language can be extended by the <strong>in</strong>clusion of a<br />

package that provides a new type Rational. Instances of this type may be used as if they were normal numeric<br />

values such as <strong>in</strong>teger.<br />

The follow<strong>in</strong>g extension to the language <strong>Ada</strong> is created by def<strong>in</strong><strong>in</strong>g the class Rational. This class def<strong>in</strong>es<br />

the follow<strong>in</strong>g operations: +, -, * on an <strong>in</strong>stance of a Rational number.<br />

The responsibilities of this class are as follows::<br />

Method Responsibility<br />

+ Delivers the sum of two rational numbers as a rational number.<br />

- Delivers the difference of two rational numbers as a rational<br />

number.<br />

* Delivers the product of two rational numbers as a rational<br />

number.<br />

/ Delivers the division of two rational numbers as a rational<br />

number.<br />

In addition the follow<strong>in</strong>g methods are used to create a rational constant and to help output a rational number <strong>in</strong><br />

a canonical form.<br />

Method<br />

Rat_Const<br />

Image<br />

Responsibility<br />

Creates a rational number from two Integer numbers.<br />

Returns a str<strong>in</strong>g image of a rational number <strong>in</strong> the canonical<br />

form ‘a b/c’. For example:<br />

Put( Image( Rat_Const(3,2) ) );<br />

would pr<strong>in</strong>t<br />

1 1/2<br />

12.2.1 <strong>Ada</strong> specification of the package<br />

The specification of the package is as follows:<br />

package Class_Rational is<br />

type Rational is private;<br />

function "+" ( F:<strong>in</strong> Rational; S:<strong>in</strong> Rational ) return Rational;<br />

function "-" ( F:<strong>in</strong> Rational; S:<strong>in</strong> Rational ) return Rational;<br />

function "*" ( F:<strong>in</strong> Rational; S:<strong>in</strong> Rational ) return Rational;<br />

function "/" ( F:<strong>in</strong> Rational; S:<strong>in</strong> Rational ) return Rational;<br />

function Rat_Const( F:<strong>in</strong> Integer;<br />

S:<strong>in</strong> Integer:=1 ) return Rational;<br />

function Image( The:<strong>in</strong> Rational ) return Str<strong>in</strong>g;<br />

private<br />

type Rational is record<br />

Above : Integer := 0; --Numerator<br />

Below : Integer := 1;<br />

end record;<br />

end Class_Rational;<br />

--Denom<strong>in</strong>ator<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!