2.) Postfix to infix translator (NOTE: This is NOT the evaluator you have already coded!) •...

90.2K

Verified Solution

Question

Programming

2.) Postfix to infix translator (NOTE: This is NOT the evaluatoryou have already coded!) • Create a java class calledPostfixToInfixTranslator that includes a main method. • The codeyou write should prompt the user for an expression in postfixnotation and use ArrayStack to output the equivalent expression ininfix. • See the following session: Enter a postfix expression: 3 4+ 2 * In infix notation that is: ((3+4)*2) Translate anotherexpression [y/n]? y Enter a postfix expression: 4 2 + 3 5 1 - * +In infix notation that is: ((4+2)+(3*(5-1))) Translate anotherexpression [y/n]? n • NOTE: The postfix expression has to acceptspaces, double digit numbers, and negative numbers. So .charAt() isnot a good approach. Maybe a scanner on a string?

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

StackADT, EmptyCollectionException, and ArrayStack code alreadybelow, please use these.


public interface StackADT //Provide layout of what we wantsomething 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 implements StackADT//We agree to supply all functions that we say exist StackADTFunctions 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 orderfor 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 newEmptyCollectionException();
       --top;
       T result = stack[top];
       stack [top] = null; // Top beforethat ends up being empty; Makes it clean
       return result;
   }
  
   public T peek() throws EmptyCollectionException
   {
       if(isEmpty())
           throw newEmptyCollectionException();
       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)
//           returntrue;
//       else
//           returnfalse;
   }
  
   public void expandCapacity()
   {
       stack = Arrays.copyOf(stack,stack.length * 2); //Would values stay the same but only place inmemory 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'tknow how to allow spaces, double digits, and negative numbers, andalso how to loop the scanner input.

import java.util.Scanner;
import java.util.Stack;

public class PostfixToInfixTranslator
{

   public static void main(String[] args) throwsEmptyCollectionException
   {
       PostfixToInfixTranslator obj = newPostfixToInfixTranslator();
       Scanner sc = newScanner(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=='^')
           returntrue;
       return false;
   }
  
   public String convert(String postfix) throwsEmptyCollectionException
   {
       ArrayStack s = newArrayStack<>();
      
       for(int i =0; 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();
   }

}


Answer & Explanation Solved by verified expert
4.3 Ratings (808 Votes)
import javaioBufferedReaderimport javaioIOExceptionimport javaioInputStreamReaderpublic class PostfixToInfixTranslator public static void mainString args throws EmptyCollectionException    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students