Question: InfixExpressionEvaluator.java public class InfixExpressionEvaluator { /** * prevent instantiation */ private InfixExpressionEvaluator() { // can't instantiate an InfixExpressionEvaluator } // end constructor /** * Evaluates

 InfixExpressionEvaluator.java public class InfixExpressionEvaluator { /** * prevent instantiation */ private

InfixExpressionEvaluator.java

public class InfixExpressionEvaluator {

/** * prevent instantiation */ private InfixExpressionEvaluator() { // can't instantiate an InfixExpressionEvaluator } // end constructor

/** * Evaluates an infix arithmetic expression containing unsigned decimal operands and a * combination of operators ({@code +, -, *, /}) including parenthesized * (sub-)expressions. All operations are performed as integers (no fractional values). * * @param expression * an infix expression composed of unsigned decimal operands and a combination * of operators ({@code +, -, *, /}) including parenthesized (sub-)expressions * @return the result of evaluating {@code expression} * @throws ArithmeticException * if {@code expression} is syntactically invalid, is null/0-length, or * attempts to divide by zero */ public static long evaluate( String expression ) throws ArithmeticException { // STUB implement this return 0 ; } // end evaluate()

public static void main( String[] args ) { // STUB for testing

} // end main()

} // end class InfixExpressionEvaluator

VectorStack.java

public final class VectorStack implements StackInterface { private Vector stack; // Last element is the top entry in stack private boolean initialized = false; private static final int DEFAULT_CAPACITY = 50; private static final int MAX_CAPACITY = 10000;

public VectorStack() { this(DEFAULT_CAPACITY); } // end default constructor

public VectorStack(int initialCapacity) { checkCapacity(initialCapacity); stack = new Vector(initialCapacity); // Size doubles as needed initialized = true; } // end constructor

// 6.17 public void push(T newEntry) { checkInitialization(); stack.add(newEntry); } // end push

// 6.18 public T peek() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else return stack.lastElement(); } // end peek

// 6.19 public T pop() { checkInitialization(); if (isEmpty()) throw new EmptyStackException(); else return stack.remove(stack.size() - 1); } // end pop

// 6.20 public boolean isEmpty() { return stack.isEmpty(); } // end isEmpty

// 6.20 public void clear() { stack.clear(); } // end clear // Throws an exception if this object is not initialized. private void checkInitialization() { if (!initialized) throw new SecurityException ("VectorStack object is not initialized properly."); } // end checkInitialization // Throws an exception if the client requests a capacity that is too large. private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) throw new IllegalStateException("Attempt to create a stack " + "whose capacity exceeds " + "allowed maximum."); } // end checkCapacity } // end VectorStack

Modify the provided InfixExpressionEvaluator.java: implement evaluate() to evaluate the provided expression consisting of: o single-digit operands the operators: +, and o parentheses you must use two stacks (use the provided VectorStack.java): o one for operands (VectorStack) o one for operators (VectorStack) do not convert the infix expression to postfix then evaluate the postfix expression all operations, including division, are integer operations (and results are integers) - use type long unary operators (e.g. -2) are illegal the input expression contains no embedded spaces and no illegal characters the input expression is a syntactically correct infix expression division by zero will not occur (consider how you can remove this restriction) Your implementation must successfully complete all tests run by TestInfixExpressionEvaluator.java. By default, this test program will only provide evaluate) with valid expressions containing only single-digit operands. For extra credit, enhance evaluate() to handle multi-digit operands. For even more extra credit, enhance evaluate() to handle invalid expressions: unbalanced parentheses null/o-length expressions consecutive operators division by zero unexpected characters Modify the provided InfixExpressionEvaluator.java: implement evaluate() to evaluate the provided expression consisting of: o single-digit operands the operators: +, and o parentheses you must use two stacks (use the provided VectorStack.java): o one for operands (VectorStack) o one for operators (VectorStack) do not convert the infix expression to postfix then evaluate the postfix expression all operations, including division, are integer operations (and results are integers) - use type long unary operators (e.g. -2) are illegal the input expression contains no embedded spaces and no illegal characters the input expression is a syntactically correct infix expression division by zero will not occur (consider how you can remove this restriction) Your implementation must successfully complete all tests run by TestInfixExpressionEvaluator.java. By default, this test program will only provide evaluate) with valid expressions containing only single-digit operands. For extra credit, enhance evaluate() to handle multi-digit operands. For even more extra credit, enhance evaluate() to handle invalid expressions: unbalanced parentheses null/o-length expressions consecutive operators division by zero unexpected characters

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!