Java Reference
In-Depth Information
Table 6.3 Command of the textual interface
Store an operand
Op <number>
Sum the last two operands
Subtract the last operand from the previous one
Multiply the last two operands
Divide the last two operands
Switch to fixed point format
Switch to floating point format
Switch to rational format
Print the list of commands
Terminate execution
Decision point
How does the calculator handle format conversion?
According to the problem specification, the calculator must handle three
number formats: fixed point, floating point and fractional. Apparently, the
simplest solution to format conversion consists in mapping each format to
the other two. This is not a viable solution, if we think that the calculator
might require maintenance, e.g. the introduction of a new number format
(say, the Roman number format). In that case, the number of mapping rela-
tions between formats would grow exponentially.
A better solution consists in defining an internal number format that is
used to store operands and results in the calculator registers. Conversion
mapping is thus required only among the user-selected format and the
internal format, and vice versa. The number of mapping relations between
formats grows linearly with the number of number formats.
Decision point
How does the calculator modify the operand parsing and result printing
behaviour according to the current selected format?
There are two possible ways to change behaviour dynamically
(Sidebar 6.1). We decide to choose the one that enforces reusability and
easy maintenance. This approach leads to the classes shown in the class
diagram of Figure 6.4. Class Command implements the command interpreter
that parses the user commands and invokes the corresponding methods of
class Calculator .
According to Figure 6.4 number formats are represented by three classes:
FloatingPointFormat , FixedPointFormat , FractionalFormat . These classes extend
the abstract class Format . Class Command has the responsibility to create an
Search WWH ::

Custom Search