Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

Differentiate between common stock and preferred slock. LO1.

Answered: 1 week ago

Question

Describe effectiveness of reading at night?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

=+What about SRI funds? Why, or why not?

Answered: 1 week ago