CSci 581-01: Special Topics in Computer Science
Object-Oriented Design & Programming
Fall Semester 1997

Assignment #6
Due Midnight, Tuesday, 2 December


General Description

This assignment requires you to design, implement, test, and document an object-oriented Java program to input, evaluate, and output arithmetic expressions. The program should handle the following types of prefix-form expressions:

An example of this type of expression is:

    sum( 2, prod( 3, 4, -2 ), diff( 3, quot( 8, 2 )), 10 )


Tasks

  1. Design and implement a base class Expr with instance methods eval and toString:

    eval
    evaluates the expression (i.e., the Expr object, not the String given as input) and returns the resulting value as a double.
    toString
    converts the expression (i.e., the Expr object) to a String in prefix form (as shown above) and returns it.

  2. Design and implement subclasses Numeric, Sum, Product, Difference, and Quotient of Expr.

    The subclasses should override eval and toString as appropriate. Your design should take advantage of polymorphism.

    A Numeric object should store the number as a double internally. The other subclasses should use appropriate collections of Expr objects.

  3. Design and implement a class (i.e., static) method parse of Expr that takes a String and returns the equivalent Expr object. Hint: You may want to implement this method as a simple recursive descent recognizer for the expression language. You may also find the class java.util.StringTokenizer or the class java.io.StreamTokenizer useful.

  4. Design and implement an instance method toInfix that converts the expression to standard algebraic infix notation, such as
        2 + 3*4*(-2) + (3-8/2) + 10
    
    The challenge is not to print unnecessary pairs of parentheses.

  5. Test your program appropriately.

  6. Document your program for use with the javadoc utility. Generate the javadoc documentation.

  7. Submit a paper listing of your program, testing output, and javadoc documentation. Also email your source code to cs581@hal.cs.olemiss.edu.


UP to CSci 581 Assignments page?


Copyright © 1998, H. Conrad Cunningham
Last modified: 12 January 1998.