Need help with the following java code, I have most of the code done but i have
ID: 3888675 • Letter: N
Question
Need help with the following java code, I have most of the code done but i have some doubts that im getting the correct output and I need help fixing the code to solve these minor issues. I also 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.
// 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.september;
/**
*
* @author Sam
*/
// Driver class
import java.io.*;
import java.util.*;
public class Driver {
/*
* @param args main argument to read command line
*/
public static void main(String[]args) throws IOException
{
Scanner fileScan = new Scanner(new File("temp1"));
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
class EvalPostfix {
private ObjectStack objStack;
public EvalPostfix()
{
objStack = new ObjectStack();
}
/*
* @param postfix space seperated postfix expression
* @return evaluated postfix expression in String format
*/
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
class InfixToPostfix {
private ObjectStack objStack;
public InfixToPostfix()
{
objStack = new ObjectStack();
}
/*
* @param infix space seperated postfix expression
* @return postfix expression equivalent to the infix
*/
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;
}
/*
* @param ch binary operator
* @return priority associated with the operator
*/
private int getPriority(char ch)
{
switch(ch)
{
case '^':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
}
// ObjectStack class
class ObjectStack implements ObjectStackInterface {
private Object[] item;
private int top;
public ObjectStack()
{
item = new Object[1];
top = -1;
}
/*
* @return boolean indicating stack is empty or not
*/
public boolean isEmpty()
{
return top == -1;
}
/*
* @return boolean indicating stack is full
*/
public boolean isFull()
{
return top == item.length -1;
}
/*
*
*/
public void clear()
{
item = new Object[1];
top = -1;
}
/*
* @param o some random object
*/
public void push(Object o)
{
if(isFull())
resize(2 * item.length);
item[++top] = o;
}
/*
* @param size size of array stack changes
*/
private void resize(int size)
{
Object[] temp = new Object[size];
for(int i = 0;i <=top;i++)
temp[i] = item[i];
item = temp;
}
/*
* @return top element of the stack and removes it
*/
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;
}
/*
* @return top item in the stack
*/
public Object top()
{
if(isEmpty())
{
System.out.println("Stack Underflow");
System.exit(1);
}
return item[top];
}
}
// ObjectStackInterface class
interface ObjectStackInterface {
public boolean isEmpty();
public boolean isFull();
public void clear();
public void push(Object o);
public Object pop();
public Object top();
}
PLEASE note that your code does not take input as described in the question. Fix that if needed
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.