Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*** Below I will add source code. You have to write code wherever stub is written. AlsoYou just have to write codes on InfixEvaluator.java only.

image text in transcribed

*** Below I will add source code. You have to write code wherever stub is written. AlsoYou just have to write codes on InfixEvaluator.java only. The rest of files are for references.

InfixEvaluator.java

public class InfixEvaluator { // Calculates the result of the exp parameter and returns the result public static int evalExpression(String exp) { //STUB return -1; } public static void main(String[] args) { // Implement your console read/write loop in main // input... evalExpression("pass the input as a string"); // output... } }

Stackinterface.java

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. */ 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.Vector;

import java.util.EmptyStackException;

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

1.4.1 Infix Calculator Write a program that implements a simple calculator with the following assumptions 1. Unary operators (as in -2) are illegal 2. All operations, including division, are integer operations. 3. The input expression contains no spaces and no illegal characters 4. The input expression is a syntactically correct infix expression (you don't have to verify this) 5. The input values range from 0-9. Multi-digit values are illegal 6. The operators have their standard precedence The calculator should behave as follows, in a loop: 1. Prompt the user to enter an infix expression, with some details about what operators are valid. 2. Read in the expression. 3. Evaluate the result of the expression. 4. Display the result on the screen (or print out E if division by 0 occurred) The only valid characters are: 0123456789+-*/OQ. The Q is only valid if it appears alone on a line, and it signals to quit the program. You are not responsible for handling invalid expressions. You may implement some error-handling, or you may let your program crash or return invalid results Example run Enter an infix expression using +, -, *, /, or O (or Q to quit): 5* (3+2) 25 2+2+2+2+2/2 Exiting program

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 Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions

Question

d. How will lack of trust be handled?

Answered: 1 week ago

Question

b. Does senior management trust the team?

Answered: 1 week ago