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:
Numeric expressions with syntax the same as
(optionally signed) Java int constants.
Sum expressions with syntax
sum(e1,e2,...,en) where
the ei are nested expressions.
Product expressions with syntax
prod(e1,e2,...,en) where
the ei are nested expressions.
Difference expressions with syntax
diff(e1,e2) where
e1 and e2 are nested
expressions.
Quotient expressions with syntax
quot(e1,e2) where
e1 and e2 are nested
expressions.
An example of this type of expression is:
sum( 2, prod( 3, 4, -2 ), diff( 3, quot( 8, 2 )), 10 )
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.
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.
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.
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.
javadoc
utility. Generate the javadoc documentation.
javadoc documentation. Also email your source code to cs581@hal.cs.olemiss.edu.
UP to CSci 581 Assignments page?