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() { // 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
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 (VectorStackStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started