Question: JAVA: I have been having issues understanding my code, can you show how the code should look for a ExpressionTree and ExpressionTestTree: Instructions - Process

JAVA: I have been having issues understanding my code, can you show how the code should look for a ExpressionTree and ExpressionTestTree:
Instructions - Process strings representing a postfix expression. Implement:
a method that parses a postfix expression and builds an expression tree
a method that given an expression tree, returns a string in infix notation with parenthesis to eliminate ambiguity
a method that given an expression tree, evaluates the expression and returns a value.
Create a class called ExpressionTree that takes a string in postfix notation as an argument to the constructor. This class will store this expression and have methods that process the expression. The expression is readonly, can't be changed and the object of type ExpressionTree will represent that particular expression that is passed as argument to the constructor.
Define these methods:
ExpressionTree(String postfix): Constructor, stores the postfix expression in an instance variable for use in other methods of this class.
boolean parse(): Method that parses the postfix returning true if the expression is in valid postfix notation (more on that below). If the expression is valid, the routine, internally should build a BinaryNode tree and store the root in an instance variable.
BinaryNode getRoot(): Returns the root of the tree or null if either the tree hasn't been built yet (i.e., parse() has not been called) or if parse() returns false negative and thus there is no tree.
int evaluate() throws ArithmeticExpression: Method evaluates the tree stored internally and returns an integer representing the value of the expression. The expression should support addition (+), multiplication (*), subtraction (-) and division(/). Note that division requires two special cases. First, check that you don't do division by 0 and second, make sure that the division is done using integer division (casting values) so that the result is always inter. This expression evaluator is an integer expression evaluator. What to do when you divide by 0? Do the same that Java does, throw an exception (ArithmeticException). You must declare this method to thrown this exception.
String infixNotation(): Return an infix notation with parenthesis to enforce order of operations. The terminal nodes of the tree contain values (i.e., number) and thus should not be wrapped in parenthesis. But all other subtrees should be turned into a parenthesized expression. This is in effect an in-order traversal of the tree.
Algorithm to parse postfix
The algorithm to parse a postfix expression uses a stack to store values as it is processing the postfix expression. For this assignment, stack will store BinaryNode objects. Thus you need a local variable in the parse() method of type Stack>.
Testing
Write test cases for this class that verifies that all the methods work as expected. Your test cases should be stored in a file called ExpressionTreeTest and call all methods in the class. The table below shows some cases that you should consider. What other cases are needed?
Postfix | Infix | Evaluation
34+|(3+4)|7
34*5+|((3*4)+5)|17
345*+|(3+(4*5))|23
10|10|10
23+45*+5/|(((2+3)+(4*5))/5)|5
Be strategic on how you build this class and how you test it. Define as many test cases as you think are needed to fully test the behavior of the project. Implement the methods, and run the test cases. Start with the constructor, the parse() method and the getRoot() methods. Then move to the evaluate() and make sure all test cases pass. Then write the infix conversion and test that. Build strong test cases to ensure you build all possible cases.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!