Question
Write in Java Create a class called ConvertExp . This class should have a method called isOperator that takes a string with one character. isOperator
Write in Java
Create a class called ConvertExp. This class should have a method called isOperator that takes a string with one character. isOperator should implement a HashSet to determine if the string passed in is one of the following operators { + , - , * , / } and return true if the string is an operator.
This part must be implemented using a Stack.
Add a method called preToInfix to ConvertExp that takes a String representation of a prefix expression and converts it into an equivalent Infix expression.
For example: Prefix Input : *+AB-CD
Infix Output : ((A+B)*(C-D))
Prefix Input : *-A/BC-/AKL
Infix Output : ((A-(B/C))*((A/K)-L))
Algorithm for Prefix to Infix:
Read the Prefix expression in reverse order (from right to left)
If the symbol is an operand, then push it onto the Stack
If the symbol is an operator, then pop two operands from the Stack
Create a string by concatenating the two operands and the operator between them. string = (operand1 + operator + operand2)
And push the resultant string back to Stack (more on next page)
Repeat the above steps until end of Prefix expression.
If the prefix expression is malformed, return Malformed expression: + the prefix expression. For example, Malformed expression: *****. (Hint: check for empty stack and/or catch EmptyStackException.)
This part must be implemented using a Stack. If you do not use a stack and the autograder issues the points, the instructor will take the points off afterwards.
Add a method called postToInfix to ConvertExp that takes a String representation of a postfix expression and converts it into an an equivalent Infix expression.
For example:
postfix Input : abc++
infix Output : (a + (b + c)
postfix Input : ab*c+
infix Output : ((a*b)+c)
If the postfix expression is malformed, return Malformed expression: + the postfix expression.
For example, Malformed expression: *****. (Hint: check for empty stack and/or catch EmptyStackException.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started