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

Need help with the following java code, I have most of the code done but i have

ID: 3870045 • Letter: N

Question

Need help with the following java code, I have most of the code done but i have some doubts that I'm getting the correct output and I need help revising my code and fixing it to meet the required instructions in case I missed any of the requirements needed to complete this lab. Also, I don't need Javadoc, I have it but forgot to include it as well.

// Driver class

import java.io.*;

import java.util.*;

public class Driver {

   public static void main(String[]args) throws IOException

   {

       Scanner fileScan = new Scanner(new File("infix.txt"));

       InfixToPostfix infixToPostfix = new InfixToPostfix();

       EvalPostfix evalPostfix = new EvalPostfix();

  

      {

         try {

          File file = new File("infix.txt");

          if (file.createNewFile()){

          System.out.println("File is created!");

          }else{

          System.out.println("File already exists.");

          }

         } catch (IOException e) {

          e.printStackTrace();

       }

  

   }

      

       while(fileScan.hasNextLine())

       {

           String infix = fileScan.nextLine();

           String postfix = infixToPostfix.convertToPostfix(infix);

           String result = evalPostfix.evalPostfix(postfix);

          

           System.out.println("Infix: "+ infix);

           System.out.println("Postfix: "+ postfix);

           System.out.println("Result: "+ result);

           System.out.println();

   }

}

}


// EvalPostFix class

public class EvalPostfix {

       private ObjectStack objStack;

       public EvalPostfix()

       {

          objStack = new ObjectStack();

       }

       public String evalPostfix(String postfix)

       {

          int op1 = 0, op2 = 0;

          int result = 0;

          String str = null;

      

      

          String[] tokens = postfix.split(" ");

          for(int i = 0; i < tokens.length; i++)

          {

       str = tokens[i];

       if(!("+".equals(str) || "-".equals(str) || "*".equals(str) || "/".equals(str) || "^".equals(str)))

       {

          objStack.push(str);

       }

       else

       {

          op1 = Integer.valueOf((String) objStack.pop());

          op2 = Integer.valueOf((String) objStack.pop());

      

          switch(str)

          {

       case "+":

          result = (op2 + op1);

          break;

       case "-":

          result = (op2 - op1);

          break;

       case "*":

          result = (op2 * op1);

          break;

       case "/":

          result = (op2 / op1);

          break;

       case "^":

          result = (int) Math.pow(op2, op1);

          break;

          }

          objStack.push("" + result);

       }

          }

      

          String val = (String) objStack.pop();

          return val;

       }

      

}


// InfixToPostfix class

public class InfixToPostfix {

       private ObjectStack objStack;

       public InfixToPostfix()

       {

          objStack = new ObjectStack();

       }

       public String convertToPostfix(String infix)

       {

          String[] tokens = infix.split(" ");

          String postfix = "";

      

          for(int i = 0; i < tokens.length; i++)

          {

       char ch = tokens[i].charAt(0);

         

       if(ch <= '9' && ch >= '0')

       {

          postfix += ch + " ";

       }

         

       else if(ch == '^' || ch == '/' || ch == '*' || ch == '+' || ch == '-' || ch == '(')

       {

          while(!objStack.isEmpty() && (getPriority(ch) <= getPriority((Character) objStack.top())) && (ch != '('))

          {

       postfix += objStack.pop() + " ";

          }

          objStack.push(ch);

       }

       else if(ch == ')')

       {

          while((!(objStack.top().equals('('))))

          {

       postfix += objStack.pop() + " ";

          }

          objStack.pop();

       }

          }

      

          while(!objStack.isEmpty())

          {

       postfix += objStack.pop() + " ";

          }

      

          return postfix;

       }

       private int getPriority(char ch)

       {

          switch(ch)

          {

       case '^':

          return 3;

       case '/':

       case '*':

          return 2;

       case '+':

       case '-':

          return 1;

       default:

          return 0;

          }

       }

}


// ObjectStack class

public class ObjectStack implements ObjectStackInterface {

   private Object[] item;

   private int top;

   public ObjectStack()

   {

       item = new Object[1];

       top = -1;

   }

   public boolean isEmpty()

   {

       return top == -1;

   }

   public boolean isFull()

   {

      return top == item.length -1;

   }

   public void clear()

   {

       item = new Object[1];

       top = -1;

   }

   public void push(Object o)

   {

       if(isFull())

           resize(2 * item.length);

           item[++top] = o;

   }

   private void resize(int size)

   {

       Object[] temp = new Object[size];

       for(int i = 0;i <=top;i++)

           temp[i] = item[i];

       item = temp;

   }

   public Object pop()

   {

       if(isEmpty())

       {

           System.out.println("Stack Underflow");

           System.exit(1);

       }

       Object temp = item[top--];

       if(top == item.length/4)

           resize(item.length/2);

       return temp;

   }

   public Object top()

   {

       if(isEmpty())

       {

           System.out.println("Stack Underflow");

           System.exit(1);

       }

  

   return item[top];

   }

}


// ObjectStackInterface class

import java.io.File;

import java.io.IOException;

public interface ObjectStackInterface {

   public boolean isEmpty();

   public boolean isFull();

   public void clear();

   public void push(Object o);

   public Object pop();

   public Object top();

}

omputer Lab: Infix to Postfix Notation One common way for a comp language instructions to evaluate arithmetic or Boolean expressions, inole conversion of the expression from infix to post require a fully parenthesized expression as injp priorities which indicate the order in which operators will be applied within a of parentheses (the operator with highest priority is evaluated first) iler for a high-level language to generate ma x. Typically, the compiler d ves oes S a s not but instead has a table of For example, consider a compiler with the following set of priorities: Priority 6 Operator unary +, unary 3 and or Then the expression: will be evaluated as: (A (B C))(DE))(A C) and its postix form would be: A BCA DE +AC

Explanation / Answer

// some changes in InfixToPostfix class


public class InfixToPostfix {
private ObjectStack objStack;
public InfixToPostfix()
{
objStack = new ObjectStack();
}
public String convertToPostfix(String infix)
{
String[] tokens = infix.split(" ");
String postfix = "";

for(int i = 0; i < tokens.length; i++)
{
char ch = tokens[i].charAt(0);

//code change from here
char ch1 = ' ';
if(i!=0)
{
ch1 = tokens[i-1].charAt(0);
}

if(((ch == '^' || ch == '/' || ch == '*' || ch == '+' || ch == '-' ) && (ch1 == '^' || ch1 == '/' || ch1 == '*' || ch1 == '+' || ch1 == '-' )) || ((ch <= '9' && ch >= '0') && (ch1 <= '9' && ch1 >= '0')))
{
System.out.println("Infix expression is not correct");
break;
}
//to here.... know code is able to find out the incorrect infix expression too


else if(ch <= '9' && ch >= '0')
{
postfix += ch + " ";
}

else if(ch == '^' || ch == '/' || ch == '*' || ch == '+' || ch == '-' || ch == '(')
{
while(!objStack.isEmpty() && (getPriority(ch) <= getPriority((Character) objStack.top())) && (ch != '('))
{
postfix += objStack.pop() + " ";
}
objStack.push(ch);
}
else if(ch == ')')
{
while((!(objStack.top().equals('('))))
{
postfix += objStack.pop() + " ";
}
objStack.pop();
}
}

while(!objStack.isEmpty())
{
postfix += objStack.pop() + " ";
}

return postfix;
}
private int getPriority(char ch)
{
switch(ch)
{
case '^':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote