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()
/** * @param args * -unused- */ public static void main( String[] args ) { // STUB for testing
} // end main()
} // end class InfixExpressionEvaluator
StackInterface.java
package edu.wit.dcsn.comp2000.stack.common;
import java.util.EmptyStackException ;
/** * An interface for the ADT stack. * * @author Frank M. Carrano * @author Timothy M. Henry * @version 4.0 * * @param
/** * Adds a new entry to the top of this stack. * * @param newEntry * An object to be added to the stack. */ public void push( T newEntry ) ;
/** * Removes and returns this stack's top entry. * * @return The object at the top of the stack. * @throws EmptyStackException * if the stack is empty before the operation. */ public T pop() ;
/** * Retrieves this stack's top entry. * * @return The object at the top of the stack. * @throws EmptyStackException * if the stack is empty before the operation. */ public T peek() ;
/** * Detects whether this stack is empty. * * @return True if the stack is empty. */ public boolean isEmpty() ;
/** Removes all entries from this stack. */ public void clear() ;
} // end StackInterface
VectorStack.java
import java.util.EmptyStackException ; import java.util.Vector ;
import edu.wit.dcsn.comp2000.stack.common.StackInterface ;
/** A class of stacks whose entries are stored in a vector. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ 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