Java Reference

In-Depth Information

instance of these classes and to pass them to class
Calculator
according to the

user-selected format.

Decision point

Which format is best suited to internal representation of operands and results?

Since the specification of the problem insists on the importance of the

fractional format, top priority is given to the accuracy of representation of

rational numbers (i.e. those represented by fractions). To simplify the con-

version procedure we decide to adopt a unique internal representation

format. Consequently, we decide to store the numbers internally using the

fractional format.

The internal structure of the calculator component is shown in Figure 6.5.

The main class is
Calculator
, which implements the main feature of the

calculator, i.e. the four operations and the management of the operands.

The operands are implemented by the
Rational
class. This class represents

a number as a fraction, i.e. it stores a numerator and a denominator. It

provides methods to add, subtract, multiply and divide two
Rational
objects.

The
Calculator
class has two associations to class
Rational
to represent the

first and second operand. Since at initialization time there are no assigned

values for the operands we decide to adopt zero as the default value of

Rational
.

In this prototype we assume that the standard base is the decimal base.

Decision point

How do the abstract class
Format
and its subclasses express the format

conversion behaviour?

Calculator

Rational

operand_1

numerator

denominator

addOperand()

add()

divide()

subtract()

multiply()

setFormat()

getFormat()

operand_2

plus()

minus()

mul()

div()

<<abstract>>

Format

parse(number : String) : Rational

toString(number : Rational) : String

getName() : String

Figure 6.5
Class relations for format conversion