Java Reference

In-Depth Information

base

operand_1

<<
abstract
>>

Base

Calculator

Rational

operand_2

format

<<
abstract
>>

Format

DecimalBase

BinaryBase

HexBase

FractionalFormat

FixedPointFormat

FloatingPointFormat

Figure 6.8
Calculator with format and base classes

:
Base

3 : parse()

1 : addOperand()

2 : parse()

command :

Command

calc

calc :
Calculator

format

:
Format

4 : new

:
Rational

Figure 6.9
Collaboration for adding a new operand

new operand (1). The
Calculator
invokes the
parse()
method on the
Format

object and passes the
Base
object as a parameter (2). Thus
Format
is able to

call the
parse()
method on the base to convert the number using the given

number base (3). Finally the method
parse()
of
Format
creates a new
Rational

object.

6.5.3

Implementation

Class
Base
implements the generic concept of number base, it provides two

essential methods:
parse()
converts a string representation of a number into

a double,
toString()
does the opposite. The former is based on Equation 6.1,

while the latter uses Equation 6.2 and Equation 6.4. These methods can be

customized using two parameters: the base and the digits; they are stored in

the homonymous attributes and are initialized through the constructor.