CS 112
Homework 6
Due: Wednesday 3/23/2005 by 12:00 p.m.
Before you start working on the homework
Look at the
applet
for arithmetic expressions, to see how the stack expands when an
expression is being evaluated. To see what expression is
currently being evaluated, open the Java Console. When you click on New in the
applet, a new
expression is chosen to be evaluated. These expressions are
exactly those on which you will be testing your program for this
homework.
Part I
Implement in Java a class ArithExpr for a very restricted form of
arithmetic expressions in postfix form. The class should have the following structure.
- As private members of the class, an array of Objects (the
arithmetic expression is stored as a sequence of integers and operators) and its length.
Your expressions will be built up from atoms which are "numbers"
and the arithmetic operators +, -, * and /
- A constructor, initializing the expression to be empty (i.e. its length is 0).
- A member function for initializing the array by reading the expression
from (a single line of) the standard input.
- A member function for printing the expression to the standard output.
- A member function for evaluating the arithmetic expression, using the
algorithm described in class. This should
make use of one of the Stack classes
that you created and/or tested in Hw 5.
Careful: the expression is a
sequence of Objects, but the values you push onot the stack, some resulting
from evaluating a subexpression of an arithmetic expression, are
integers.
- Design a test driver for your class, as a Java application. Test your program on a few arithmetic
expressions with at least 5 operators.
- Test tour program on all the expressions given in the following data file.
Notice that each arithmetic expression occupies at most one line. Make the assumption that this is indeed the case,
when you design the method reading an expression from the standard input.
Part II: Calculator
Design an applet with a GUI for a Pocket Calculator to evaluate arithmetic expressions in postfix form.
The GUI should have the look of a real calculator, with buttons for punching in digits and operators and a textfield where the
expression is displayed. The user will type in an arithmetic expression in postfix form. When a button marked with the equal
sign is pressed, the expression is evaluated and the result is displayed.
Test your
program on all the arithmetic expressions illustrated by the
applet shown in class (look in the Java console).
Minimal interface
Do not start working on all the complex features of the described program. Instead, you may choose to focus on the important
aspects of the homework and postpone some other features till the last versions. Here are a few examples:
- Develeop later the method for reading the expression from user input. You may start with some hardwired expressions,
initialized in the constructor.
- Similarly for the applet: the complete user interface, with expressions read from a text field, should be left for the
extra credit part.
- The user interface should be developed only after you have completed and tested part I. You may incorporate the features one
by ine, starting with some empty actions (or simply printing on the Java console) for some buttons.
- Start by entering the expression via button clicks. The expression should then be displayed on the Java console and, eventually,
in a text field.
Extra credit
- Extend the GUI with a text field for entering the expression. When the user presses return, the expression is evaluated.
Ileana Streinu