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()

/** * @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 * the class of objects the stack will hold */ public interface StackInterface {

/** * 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 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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions