Java Reference

In-Depth Information

Base

Decimal

Binary

Hexadecimal

Base 10

Digits 0123456789

Base 2

Digits 01

Base 16

Digits 0123456789ABCDEF

Figure 6.7
Analysis of number bases

The command line interpreter must provide the user with information

about the current base and representation format. This is a simple extension

of the information provided by the interpreter in the previous prototype.

6.5.2

Design

The design of this prototype is basically an extension of the previous one

with the addition of the classes required to handle multiple number bases.

The structure of the classes identified during analysis is quite similar to

the classes identified in the previous prototype, which were related to

representation formats. Because of this analogy we decide to adopt a similar

approach and to represent the base in use by the calculator through an

object of type
Base
. The resulting class diagram is shown in Figure 6.8.

The class
Base
defines the interface and basic behaviour, which are

common to all the concrete base classes,
DecimalBase
,
BinartyBase
and

HexBase
.

There is an important difference between the number base related class

hierarchy and the representation format related one. Class
Base
can use

generic algorithms to convert between a string and a number; therefore the

subclasses need only define the parameters for such algorithms. In the case of

format the separation is achieved by defining a specific behaviour in the con-

crete subclasses. In the case of the base, it is achieved by customizing the

generic algorithms defined in the abstract class.

The sequence of calls to add a new operand to the calculator is described

in Figure 6.9. The command class calls the
addOperand()
method to add a

Table 6.5
Additional commands

Command

Description

Dec

Switch to base 10

Bin

Switch to binary base

Hex

Switch to hexadecimal base