Project #5
Due Date: beginning of class, Thursday, April 27, 2006
NO projects can be accepted after
the April 27 deadline.
In this project you will develop an interactive application that
emulates
the operation of a calculator that has a number of registers and
supports
complex calculations. You will find Chapter 9 to be very helpful,
especially section 9.5.
Abstract description of the calculator
Our calculator has six variables or registers.
- The registers are named A, B, C, D, E, and F.
- Each register can contain an expression made up of register
names,
floating
point constants, parentheses, and the operators +, -, *, and /.
- Assignment to a register is indicated by the = operator.
- All registers contain an expression of 0.0 by default.
Further, the calculator has three commands. These are:
- CLEAR, which clears all registers
- SET, which is followed by an assignment statement
- EVAL, which is followed by a register name. EVAL
evaluates
the expression and displays the result.
Here is an example of a run of the calculator. The calculator responses
are indented.
CLEAR
OK
SET B = 3.0
OK
SET E = 5.0
OK
SET D = (B * E) / 2.0
OK
EVAL D
7.5
SET E = 4.0
OK
EVAL D
6.0
SET F = B + D)
ERROR
Implementation
Here are some implementation specifications.
- Represent the register names by a set of six integer constants A
through F:
final int A = 0;
final int B = 1;
etc.
- Represent an expression internally by an expression tree.
- Represent the registers themselves by a 6-element array of
expressions
(trees), subscripted by the register numbers 0-5.
- Each node of a tree will have left and right pointers and four
suggested data fields:
- a character "type code" indicating whether the node contains a
register
name, a float value, or an operator; use 'V', 'C',
and 'O'
for these codes
- a field for the register name
- a field for a float value
- a character field that can hold an operator
- the "type code" will indicate which of the 3 fields is actually
being
"used"
in the node
- The application will read a line of input, parse it, and act
according
to the command:
- If the command is CLEAR, clear all the registers to
empty
trees
- If the command is SET, determine which register is
being set,
then convert the expression to an expression tree using an algorithm to
be given in class.
- If the command is EVAL, evaluate the tree by an
algorithm to
be
given in class.
Other than meeting the above specs, you are free to design the
details
of your project. For example, use your judgement about how to handle
errors,
how to put methods into classes, etc. Document your design carefully;
any
correct design will be given full marks.