Programming Assignment

Due:

Please make sure you adhere to the policies on academic honesty.

Please see the general programming webpage for details about the programming environment for this course, and specifically for directions in how to submit your programming assignment electronically.

The files you need for this assignment can be downloaded here.


Contents:

  • Overview
  • Expression Format
  • Examples
  • Your task
  • Drivers
  • Files you will need
  • Files to Submit
  • Efficiency
  • BinaryTree interface
  • Recursion on BinaryTree's
  • Usefull String methods
  • Extra Credit

  • Overview

    The goal of this program will be to turn a parenthesized expression such as:
    ((((3+1)x3)/((9-5)+2)) - ((3 x (7-4))+6))
    into a valid binary tree which can then be used to evaluate the expression. This particular expression matches that from Figure 6.6 of the text. Please read the discussion of Example 6.5 on pages 232-233 of the text carefully. You will not be writing any low-level tree code, rather you will make use of a modified implementation of the BinaryTree interface from the text.


    Expression Format

    Parsers for languages such as Java must certainly be able to perform such a task. Arithmetic expression appear in source code and must be evaluated in turn. In fact, they deal with a variety of complications, as they allow unary operators (such as "-3"), and operators which take more than two (such as "4+8+2+12"). Furthermore, they enforce a prescribed precedence for operators in the case where the user does not properly parenthesize an expression (imagine "5 - 3 x 4 + 10").

    We will consider a simple version of the problem by using only binary operators and by assuming strict parenthesization in expressions. In particular we will make the following recursive definition for a well-formed expression:

    This simple recursive definition allows us to represent arbitrarily complex arithmetic expression (and understanding this recursion will allow you to write a relative simple solution to this assignment!)

    You may assume that your routine will be given WELL FORMED expressions. You do not need to be concerned with how to gracefully handle improperly formed expressions.

    Examples

    Examples of well-formed expressions include (one per line):

    13
    (5 - 8)
    (3 x (4/2))
    ((((3+1)x3)/((9-5)+2))-((3x(7-4))+6))


    Your task

    You are to complete the routines defined in file Arithmetic.java: