IN JAVA ******Complete the - evauatevPostfix and -infixToPostfix methods, you can use the helper functions above in the code if you want, use stack and queue classes *** Each array element contains a...


IN JAVA


******Complete the -evauatevPostfixand-infixToPostfixmethods, you can use the helper functions above in the code if you want, use stack and queue classes


***Each array element contains a single element of the expression


!!!!!!!!!!!!!!!!!!!!!!!!!!!! dont't create new methods!!! complelte the ones that are already created using the helper functions above or not


ABSTRACT INFIX TO POSTFIX


public abstract class AbstractInfixToPostfix {
/**
* Evaluate the result of two String operands (a & b) and a String operator and return the result as a String
* @param a - the first operand
* @param b - the second operand
* @param operator - the operator
* @return The result of "a operator b"
*/
public abstract String evaluate(String a, String b, String operator);



/**
* Returns true if the given token is an integer
* @param token - the token to test
* @return true if token is an integer, false otherwise
*/
public abstract boolean isOperand(String token);



/**
* Returns the infix ranking of the given operator, or 0 by default
* @param operator - the operator to analyze
* @return the infix rank of operator
*/
public abstract int infixOpRank(String operator);



/**
* Returns the operator stack ranking of the given operator, or 0 by default
* @param operator - the operator to analyze
* @return the operator stack rank of operator
*/
public abstract int opStackRank(String operator);



/**
* Separates a space separated mathematical expression in String representation
* and returns an array of Strings containing the individual elements
* @param expressionString - the expression in String format to separate
* @return An array of the operands and operators in expressionString
*/
public abstract String[] parseExpression(String expressionString);



/**
* Returns the string representation of an expression
* @param expression - an array of operands and operators forming a mathematical expression
* @return expression as a single, space separated String
*/
public abstract String expressionToString(String[] expression);



/**
* Evaluate a postfix expression
* Integers only, so the division operation is integer division (no remainder from division)
* @param postfixExpression - the postfix expression to evaluate
* @return The result from evaluating postfixExpression
*/
public abstract String evaluatePostfix(String[] postfixExpression);



/**
* Convert an infix expression to a postfix expression
* @param infixExpression - the infix expression to convert to postfix
* @return The postfix expression representation of infixExpression
*/
public abstract String[] infixToPostfix(String[] infixExpression);
}


STACK


public class Stack extends LinkedList {
/**
* Default Constructor
* if a constructor is not provided, Java calls the default constructor instead (no parameters)
* the constructor below can be commented out or removed without changing the behavior of this class
*/
public Stack() {
super();
}



/**
* adds an element on top of the stack (at the beginning of the linked list)
* @param element - the element to be pushed
*/
public void push(Type element) {
add(0, element);
}



/**
* returns the element on top of the stack and removes it
* @return the element on top of the stack
*/
public Type pop() {
Type element = getFirst();
remove(0);
return element;
}



/**
* returns the element on top of the stack WITHOUT removing it
* @return the element on top of the stack
*/
public Type peek() {
Type element = getFirst();
return element;
}
}


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


QUEUE


public class Queue extends LinkedList {
public void enqueue(Type element) {
add(element);
}



/**
* Removes and returns the element at the front of the queue (the beginning of the linked list)
*/
public Type dequeue() {
Type element = getFirst();
remove(0);
return element;
}



/**
* Returns the element at the front of the queue WITHOUT removing i
*/
public Type peek() {
Type element = getFirst();
return element;
}
}


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


public class InfixToPostfix extends AbstractInfixToPostfix {
public String evaluate(String a, String b, String operator) {
//parse the opperands
int numA;
int numB;
try {
numA = Integer.parseInt(a);
numB = Integer.parseInt(b);
}
catch(NumberFormatException e) {
System.out.println("Invald operand found during evaluation");
System.out.println("\ta=" + a + ", b=" + b);
return null;
}



//Addition
if (operator.equals("+")) return ("" + (int)(numA + numB));
//Subtraction
else if (operator.equals("-")) return ("" + (int)(numA - numB));
//Multiplication
else if (operator.equals("*")) return ("" + (int)(numA * numB));
//Integer Division
else if (operator.equals("/")) return ("" + (int)(numA / numB));
//Exponents
else if (operator.equals("^")) return ("" + (int)(Math.pow(numA, numB)));



else {
System.out.println("Invalid operator found during evaluation");
System.out.println("\toperator=" + operator);
return null;
}
}




public boolean isOperand(String token) {
try {
Integer.parseInt(token);
return True;
}
catch (NumberFormatException e) {
return false;
}
}




public int infixOpRank(String operator) {
if (operator.equals("(")) return 4;
else if (operator.equals("^")) return 3;
else if (operator.equals("*") || operator.equals("/")) return 2;
else if (operator.equals("+") || operator.equals("-")) return 1;
else return 0;
}




public int opStackRank(String operator) {
if (operator.equals("^") || operator.equals("*") || operator.equals("/")) return 2;
else if (operator.equals("+") || operator.equals("-")) return 1;
else return 0;
}



/**
* Separates a space separated mathematical expression in String representation
* and returns an array of Strings containing the individual elements
*/
public String[] parseExpression(String expressionString) {
//divide from single string into individual tokens
return expressionString.split(" ");
}




public String expressionToString(String[] expression) {
String expString = "";
if (expression.length > 0) {
for(int i = 0; i

expString += expression[i] + " ";
}
expString += expression[expression.length-1];
}
return expString;
}



/**
* Evaluate a postfix expression
* Integers only, so the division operation is integer division (no remainder from division)
* @param postfixExpression - the postfix expression to evaluate
* @return The result from evaluating postfixExpression
*/
public String evaluatePostfix(String[] postfixExpression) {
// COMPLETE THIS FOR PROGRAM 5 use helper functions if possible but not necessary
//return a value as a string
//input type is a string array so we need to use parse eXpreression to split it into tokens
return null;
}



/**
* Convert an infix expression to a postfix expression
* @param infixExpression - the infix expression to convert to postfix
* @return The postfix expression representation of infixExpression
*/
public String[] infixToPostfix(String[] infixExpression) {
// COMPLETE THIS FOR PROGRAM 5
//convert string array into postfix sring array
return null;
}
}

Oct 21, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here