Java Reference
In-Depth Information
calc.setFormat(
new
RationalFormat());
assertEquals("3.0/4.0",calc.secondOperand());
calc.setBase(
new
BinaryBase());
assertEquals("11.0/100.0",calc.secondOperand());
calc.setBase(
new
HexBase());
assertEquals("3.0/4.0",calc.secondOperand());
}
catch
(FormatException e)
{ fail("Unexpected exception"); }
}
}
6.6
■
Extension
The reader can extend the application presented in this chapter in several
ways:
The use of only two operands in the calculator represents a limitation
when complex expressions are needed. A more general solution consists
of using a stack: each time an operand is added it is pushed onto the stack;
when an operation is selected the operands are popped from the stack and
the result is pushed back onto the stack.
■
The calculator described here is able to handle only constant numeric
values. A powerful extension consists of adding the capability of dealing
with variables. In this case the calculator must manage symbolic expres-
sions: expressions must be stored using an internal format (an abstract
syntax tree). In addition a table must contain the values of the variables
that are used to compute the value of symbolic expressions.
■
A graphical user interface can be developed to provide a user friendly
interaction with the calculator.
■
6.7
■
Assessment
Analysis techniques
. We analysed the problem domain and identified the
main concepts; since they are correlated we abstracted concepts into more
general ones.
Modelling techniques
. In this chapter we introduced use case diagrams
and collaboration diagrams. In addition we used inheritance to factorize
common characteristics into base classes.
Development approach
. The development of the multi-format calculator
highlighted two important issues:
dynamic behaviour and polymorphism; and
■
exception handling.
■
Handling a dynamic behaviour is a common problem and the solution
adopted in this system is largely reusable. Actually it is an instance of the