x.øi5my project(part 1 of 3 ) should be able to evaluate expressions…..Well it is not…. I get an

x.øi5my project(part 1 of 3 ) should be able to evaluate expressions…..Well it is not…. I get an error with my generic stack… push,peek,pop,isEmpty is throwingan error that it can not be found….any Ideas???

import java.util.*;
public class EvaluateExpression {public static void main(String[] args) {// Check number of arguments passedif (args.length
String expression = “”;for (int i = 0; i
try {System.out.println(evaluateExpression(expression));}catch (Exception ex) {System.out.println(“Wrong expression”);}}
/** Evaluate an expression */public static int evaluateExpression(String expression) {// Create operandStack to store operandsGenericStack operandStack= new GenericStack();
// Create operatorStack to store operatorsGenericStack operatorStack= new GenericStack();
// Extract operands and operatorsjava.util.StringTokenizer tokens =new java.util.StringTokenizer(expression, “()+-/*”, true);
// Phase 1: Scan tokenswhile (tokens.hasMoreTokens()) {String token = tokens.nextToken().trim(); // Extract a tokenif (token.length() == 0) // Blank spacecontinue; // Back to the while loop to extract the next tokenelse if (token.charAt(0) == '+' || token.charAt(0) == '-') {// Process all +, -, *, / in the top of the operator stackwhile (!operatorStack.isEmpty() &&(operatorStack.peek().equals('+') ||operatorStack.peek().equals('-') ||operatorStack.peek().equals('*') ||operatorStack.peek().equals('/'))) {processAnOperator(operandStack, operatorStack);}
// Push the + or – operator into the operator stackoperatorStack.push(new Character(token.charAt(0)));}else if (token.charAt(0) == '*' || token.charAt(0) == '/') {// Process all *, / in the top of the operator stackwhile (!operatorStack.isEmpty() &&(operatorStack.peek().equals('*') ||operatorStack.peek().equals('/'))) {processAnOperator(operandStack, operatorStack);}
// Push the * or / operator into the operator stackoperatorStack.push(new Character(token.charAt(0)));}else if (token.trim().charAt(0) == '(') {operatorStack.push(new Character('(')); // Push '(' to stack}else if (token.trim().charAt(0) == ')') {// Process all the operators in the stack until seeing '('while (!operatorStack.peek().equals('(')) {processAnOperator(operandStack, operatorStack);}
operatorStack.pop(); // Pop the '(' symbol from the stack}else { // An operand scanned// Push an operand to the stackoperandStack.push(new Integer(token));}}
// Phase 2: process all the remaining operators in the stackwhile (!operatorStack.isEmpty()) {processAnOperator(operandStack, operatorStack);}
// Return the resultreturn ((Integer)(operandStack.pop())).intValue();}
/** Process one opeator: Take an operator from operatorStack and*apply it on the operands in the operandStack */public static void processAnOperator(GenericStack operandStack,GenericStack operatorStack) {if (operatorStack.peek().equals('+')) {operatorStack.pop();int op1 = ((Integer)(operandStack.pop())).intValue();int op2 = ((Integer)(operandStack.pop())).intValue();operandStack.push(new Integer(op2 + op1));}else if (operatorStack.peek().equals('-')) {operatorStack.pop();int op1 = ((Integer)(operandStack.pop())).intValue();int op2 = ((Integer)(operandStack.pop())).intValue();operandStack.push(new Integer(op2 – op1));}else if (operatorStack.peek().equals('*')) {int op1 = ((Integer)(operandStack.pop())).intValue();int op2 = ((Integer)(operandStack.pop())).intValue();operandStack.push(new Integer(op2 * op1));}else if (operatorStack.peek().equals('/')) {operatorStack.pop();int op1 = ((Integer)(operandStack.pop())).intValue();int op2 = ((Integer)(operandStack.pop())).intValue();operandStack.push(new Integer(op2 / op1));}}}
¥_¤`ø5x.øi5

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer