In: Computer Science
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<T> //Provide layout of what we want
something to do w/o supplying the way it gets done =
interface
{
public void push( T element) ;
public T pop () throws EmptyCollectionException;
public T peek() throws EmptyCollectionException;
public boolean isEmpty();
public int size();
}
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<T> implements StackADT<T>
//We agree to supply all functions that we say exist StackADT
Functions correlate to ArrayStack
{
private final static int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
public ArrayStack()
{
this(DEFAULT_CAPACITY);
}
@SuppressWarnings("unchecked")
public ArrayStack(int initialCapacity)
{
top = 0;
stack = (T[]) new Object
[initialCapacity]; //Have to cast Object Array to T array in order
for placeholder to work. Java just works like that.
}
public void push( T element)
{
if(size() == stack.length)
expandCapacity();
stack [top] = element;
++top;
}
public T pop() throws EmptyCollectionException
{
if(isEmpty())
throw new
EmptyCollectionException();
--top;
T result = stack[top];
stack [top] = null; // Top before
that ends up being empty; Makes it clean
return result;
}
public T peek() throws EmptyCollectionException
{
if(isEmpty())
throw new
EmptyCollectionException();
return stack[top -1];
}
public int size()
{
return top;
}
public boolean isEmpty()
{
// Even shorter
return top ==0;
// Short
// boolean a = top == 0;
// return a;
// long way
// if(top==0)
// return
true;
// else
// return
false;
}
public void expandCapacity()
{
stack = Arrays.copyOf(stack,
stack.length * 2); //Would values stay the same but only place in
memory changes? Hexa decimal #'s?
}
public String toString()
{
String output = "ArrayStack-> [
";
for(int i =top-1; i >=0 ;
i--)
{
output+=
stack[i].toString() + " ";
}
output+= "]";
return output;
}
}
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<String> s = new
ArrayStack<>();
for(int i =0; i <
postfix.length(); i++)
{
char c =
postfix.charAt(i);
if(isOperator(c))
{
String b = s.pop();
String a = s.pop();
s.push("("+a+c+b+")");
}
else
s.push(""+c);
}
return s.pop();
}
}
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PostfixToInfixTranslator { public static void main(String[] args) throws EmptyCollectionException { PostfixToInfixTranslator obj = new PostfixToInfixTranslator(); try { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); String cmd = "y"; String[] postfix; while (cmd.equalsIgnoreCase("y")) { System.out.print("Enter a Postfix expression : "); postfix = input.readLine().split(" "); System.out.println("Infix Notation is : " + obj.convert(postfix)); System.out.println("Translate another expression[y/n]?"); cmd = input.readLine(); } } catch (IOException ex) { } } private boolean isOperator(String c) { if (c.equalsIgnoreCase("+") || c.equalsIgnoreCase("-") || c.equalsIgnoreCase("*") || c.equalsIgnoreCase("/") || c.equalsIgnoreCase("^")) return true; return false; } public String convert(String[] postfix) throws EmptyCollectionException { ArrayStack<String> s = new ArrayStack<>(); for (int i = 0; i < postfix.length; i++) { String c = postfix[i]; if (isOperator(c)) { String b = s.pop(); String a = s.pop(); s.push("(" + a + c + b + ")"); } else s.push(c); } return s.pop(); } }