Question: 2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) Create a java class called PostfixToInfixTranslator that includes a main
2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) Create a java class called PostfixToInfixTranslator that includes a main method. The code you write should prompt the user for an expression in postfix notation and use ArrayStack to output the equivalent expression in infix. See the following session: Enter a postfix expression: 3 4 + 2 * In infix notation that is: ((3+4)*2) Translate another expression [y/n]? y Enter a postfix expression: 4 2 + 3 5 1 - * + In infix notation that is: ((4+2)+(3*(5-1))) Translate another expression [y/n]? n NOTE: The postfix expression has to accept spaces, double digit numbers, and negative numbers. So .charAt() is not a good approach. Maybe a scanner on a string?
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
StackADT, EmptyCollectionException, and ArrayStack code already below, please use these.
public interface StackADT
public class EmptyCollectionException extends Exception { private static final long serialVersionUID = 358083002087971606L;
public EmptyCollectionException() { super(); } public EmptyCollectionException(String msg) { super(msg); } }
import java.util.Arrays;
public class ArrayStack
WHAT I ALREADY HAVE CODED IN TRANSLATOR, PLEASE HELP. I don't know how to allow spaces, double digits, and negative numbers, and also how to loop the scanner input.
import java.util.Scanner; import java.util.Stack;
public class PostfixToInfixTranslator {
public static void main(String[] args) throws EmptyCollectionException { PostfixToInfixTranslator obj = new PostfixToInfixTranslator(); Scanner sc = new Scanner(System.in); System.out.print("Postfix : "); String postfix = sc.next(); System.out.println("Infix : " +obj.convert(postfix));
}
private boolean isOperator(char c) { if(c == '+' || c== '-' || c== '*' || c=='/' || c=='^') return true; return false; } public String convert(String postfix) throws EmptyCollectionException { ArrayStack
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
