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.

  1. 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 /
  2. A constructor, initializing the expression to be empty (i.e. its length is 0).
  3. A member function for initializing the array by reading the expression from (a single line of) the standard input.
  4. A member function for printing the expression to the standard output.
  5. 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.
  6. Design a test driver for your class, as a Java application. Test your program on a few arithmetic expressions with at least 5 operators.
  7. 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:

Extra credit


Ileana Streinu