This assignment has the following options. Each individual must select one of these options by Tuesday, 13 April, and stay with that choice.
For option 1, the groups may consist of two or more members of the same groups as for Assignment #2. You may not form groups whose members were in different groups for assignment #2.
The documentation on every "module" should include the name of the programmer who implemented that module.
This assignment requires you (as an individual) 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 internally.
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.
Note: The input string to parse() should not be stored in
the instances of the Expr classes.
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. Use of preconditions, postconditions, and invariants is also
recommended.
UP to CSci 581 Assignments page?