Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.
In this assignment you will write a program that will take a mathematical expression written in post-fix notation and generate an expression tree
In this assignment you will write a program that will take a mathematical expression written in post-fix notation and generate an expression tree from it. Normally, we use what's called in-fix notation like: 1 + 1. Post-fix puts the operator after the two numbers like: 1 1 +. An expression tree is a type of binary tree that has the operators as parents of the two values that go into them.
Your program must do the following in order to receive full credit on this assignment.
- Download the attached java files and get them into your project and ensure they compile.
- In the Utilities class, the isOperator method is a stub from the interface. Implement this
- method.
- The parameter s can be any string. This method should return true if s is any of
- the five basic operators.
- That is the method returns true if s is + or - or * or / or %. Otherwise it will return
- false.
- Also in the Utilities class, the allValidTerms method from the interface is also a stub.
- Implement this method as well.
a. In order for this method to return true, every element must be either a number or
an operator. If any of them are not, it should return false.
- Create a new class called NumberNode. This class will represent a leaf node at the
- bottom of the tree. It must implement the Node interface.
- NumberNode only needs an int value for an instance variable. It should have one
- constructor which takes an int parameter and stores it as this Node's value.
- You will have to implement all methods from the Node interface.
a. Since a NumberNode does not have any children, what value should getLeft and getRight return?
7. Create a new class called OperatorNode, which represents the operators stored in the tree with two Nodes under it. This should also implement the Node interface.
- This will need a string instance variable to hold its operator.
- It will also need one for each Node under it.
i. What type should these be?
8. As with NumberNode, implement the methods from the Node interface.
a. The tricky one this time is getValue. We don't have an int value to return. What do we do when when a class inherits or implements a method that it does not support?
9. As always when dealing with user input, we have no idea what they will give us. They might give us garbage, so we will need some error handling. While there are other potential ways to handle these, we will be using exceptions. To start, create a new class called OperatorOnlyException. This exception will be thrown when the user provides an expression that is only operators with no numbers in it.
- This class must inherit from RuntimeException in order to be thrown.
- Note: Some compilers may issue a warning that you have not defined a final static
- long serial number. This comes from serialization and can be ignored. You can also just define a serial number, add an annotation to suppress the warning (@suppressWarnings("serial")), or change your compiler settings to not warn you about that.
- Define a default constructor for this class. It should generate a message that an expression cannot be only operators.
- See example outputs.
- Since you are inheriting from RuntimeException, you inherit everything from that
- class. The constructor can be as short as one line. Look up the constructors for
- RuntimeException and remember how inheritance works to find the shortcut.
- Create another class called InvalidExpressionException.
a. b.
12. In the a. b. c.
This also must inherit from RuntimeException.
Again, create a single default constructor that generates a message that the expression is invalid.
ExpressionTree class implement a new method called parseExpression.
It is public and does not return a value.
It takes an array of strings as a parameter.
It can throw either of the exceptions we've defined, but it will also possibly throw an EmptyCollectionException as we'll soon see, so be sure to add a throws clause for all three of these.
13. Before we can try to parse the expression, we must first check if we even can. There are several possible situations we need to check for.
a. If the entire expression is operators, throw an OperatorOnlyException.
i. The rest of these situations will throw an InvalidExpressionException.
- If it starts with an operator.
- If the expression is even in length, it cannot be valid.
i. Might sound odd but think about it. d. If there are any invalid terms.
- Having eliminated all of those possibilities, if the expression is one term long, we can just create a new NumberNode with it and set it as our root and be done.
- Otherwise, parse the expression into the tree.
- The built in Stack class is imported for you. Think of how you can use it to track
- what will be the children of your next operator node.
- Remember, the pattern should be number number operator. After that initial part,
- the only thing that can be added would be another number followed by another operator. You can't be sure how many times this pair will repeat, but that will be the pattern.
i. Keep in mind, the left child of one of these pairs would be the OperatorNode from the previous set.
c. You should assume that this is the pattern you are given. There is a scenario that passes the above checks, but should cause you to pop from an empty stack. This is what causes the EmptyStackCollection exception to be thrown. Think about what
that scenario would be as it might help you figure out how to use the stack if
you're not sure yet.
d. At the end make sure the top Node is set as the root.
16. Implement a way to do an in-order traversal of the expression tree to generate the in-fix notation version of the expression.
a. You have two basic choices for doing this. You can write new method in ExpressionTree that will do it (recursion will help) or you can implement the provided toString method stub (This will instead have you implement toString methods in your Node classes). The choice is yours.
17. In the Assignment12 class, complete the main method to try and parse the expression.
- Insert your code at the provided spot, there should be no need to edit anything
- above or below that point.
- Make sure you catch all the potential exceptions!
- For each, print "Error!" followed by the message from the exception, and then further explain and ask the user to try again.
- See example outputs.
- If you parse it successfully, then print out the in-fix notation version of the
- expression.
Example Inputs