Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a Java class called Stackthat will have push, pop and peek methods alon

ID: 3612326 • Letter: 1

Question

1)      Write a Java class called Stackthat will have push, pop and peek methods along with theconstructor. This class should have two additional functions asfollows:

a.       String ConvertInfixToPostFix (Stringinfix) : This will transform an infix expression to apostfix expression. The input to this function will be a string (orcharacter array – whatever you choose) where each operand canbe a single digit and all operator will be binary

                                                              i.      Input     :3+7–(2*8)/8   [not as 34+7 etc and (-8) unaryoperator]

                                                            ii.      Output : 37+28*8/-

b.      int EvaluatePostFix (String postfix):This function will be responsible to to compute theresult.

This Class should have a main function and in the main function,you should use all the functions and print the values to the screenafter each function is called. Do not copy from book and do not useJava’s in-built queue class.

Explanation / Answer

Dear... Infix.java import java.util.*;
    public class Infix
{
public static final String operators ="+-*/";
public static final String left ="(<{[";
public static final String right =")>}]";
public static void main(String[]args)
{
System.out.println("Input InfixExpression: ");
Scanner yo = newScanner(System.in);
String s = yo.nextLine();
System.out.println("Postfixexpression: " + convert(s));
String yomama = convert(s);
System.out.println("Result ofPostfix Expression: " + Postfix.eval(yomama));
}
public static String convert(Stringstr)
{
Stack<Character> stuff =new Stack<Character>();
String p = "";
for(int x = 0; x <str.length(); x++)
{
char y =str.charAt(x);
if(!Postfix.isOperator(y))
p += y;
else if(leftParen(y))
  stuff.push(y);
elseif(rightParen(y))
{
  while(stuff.peek() != '(')
p +=stuff.pop();
  stuff.pop();
}

else
{
  if(!stuff.isEmpty() && !isLower(y, stuff.peek()))
stuff.push(y);
elseif(!stuff.isEmpty())
p +=stuff.pop();
else
stuff.push(y);
}
}

while(!stuff.isEmpty())
p+=stuff.pop();
return p;
}
public static boolean isLower(char c1,char c2)
{
if((c1 == '+' || c1 == '-')&& (c2 == '*' || c2 == '/'))
return true;
return false;
}
public static boolean leftParen(charch)
{
if(ch == '(')
return true;
return false;
}
public static boolean rightParen(charch)
{
if(ch == ')')
return true;
return false;
}
} b)     Postfix.java
import java.util.*;
public class Postfix
{
   public static final String operator ="+-*/";
    public static final String numbers= "1234567890";
   public static void main(String[]args)
   {
    Scanner yo = newScanner(System.in);
  String s =yo.nextLine();
   System.out.println(eval(s));
  }
public static int eval(String str)
{
    Stack<Integer>stuff = new Stack<Integer>();
  for(int x = 0; x <str.length(); x++)
  {
   char s =str.charAt(x);
   if(isOperator(s))
  {
    int la = stuff.pop();
  intlala = stuff.pop();
  intlalala = eval(lala, la, s);
   stuff.push(lalala);
}
elseif(!isOperator(s))
{
        String so = "" + s;
       stuff.push(Integer.parseInt(so));
}
}
return stuff.pop();
}
public static int eval(int a, int b,char ch)
{
  if(ch == '+')
return a + b;
else if(ch == '-')
return a - b;
else if(ch == '*')
return a * b;
else if(ch == '/')
return a/b;
return -1;
}
public static boolean isOperator(charch)
{
String yo = "" + ch;
return operator.contains(yo);
}
} I hope it is useful to you.