Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a concrete ArrayStack class that extends the Stack interface as we discussed in the class (any other different Stack class implementation, even if it

Implement a concrete ArrayStack class that extends the Stack interface as we discussed in the class (any other different Stack class implementation, even if it is implemented by yourself, will not receive any credit).

Write a method that evaluates an arithmatic expression, which is given by a string.

public class Evaluate { public static int expression(String str) { // return the value } }

Your implementation is required to use the Stack(IStack) interface we discussed in the class.

Write a necessary test program to verify the result. For example,

14-3*4+2*5-6*2 (should return 0) 

In another example,

14-3*(4+2*(5-6))*2 (should return 2) 

You may want to test all possible corner conditions to make sure your implement is inclusive.

Your implementation only considers +, - and * operators, as well as the parentheses (), and does not need to consider other operators and brackets.

****THIS IS THE STACK IMPLEMENTATION USED IN CLASS. PLEASE USE THIS STACK IMPLEMENTATION****

public interface IStack{ int size(); boolean isEmpty(); Object top(); void push(Object item) throws StackFullException; Object pop(); }

public class ArrayStack implements IStack{ private Object[] data; private int size; private static int CAP; public ArrayStack(int capacity){ data=new Object size=0; CAP=capacity; } public ArrayStack(){ this(10); } public int size(){reutrn size;} public boolean isEmpty(){ return size()==0; } public void push(Object item) throws StackFullException{ if(size==CAP) throw new StackFullException("reach the limit"); data[size++]=item; } public object pop(){ if(isEmpty()) return null; Object answer=data[size-1]; size--; return answer; } public Object top(){ if(isEmpty()) return null; return data[size-1]

}}

public class StackFullException extends Exception{ public StackFullException (String str){ System.out.println("StackFullException:" +str); } public StackFullException(){ this("No space left"); } }

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions