Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/** An interface for the ADT stack. @author Frank M. Carrano @version 4.0 */ public interface StackInterface { /** Adds a new entry to the

/** An interface for the ADT stack. @author Frank M. Carrano @version 4.0 */ 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

image text in transcribed

Create class called ArrayBasedStack that implements the StackInterface.

Declare the following variables:

data: references an array storing elements in the list

topOfStack: an int value representing the location of the stack top in the array

INITIAL_CAPACITY: the default capacity of the stack set to 5

private T] data; private int topOfStack; private static final int INITIAL-CAPACITY-5; Add a constructor that will initialize the stack with a user-defined initial capacity that must be greater than 0. If the parameter is 0 or less, throw an IllegalArgumentException with the comment "Array initial size error". The top of the stack is assigned a value of-1 to indicate that the stack is currently empty public ArrayBasedStack(int capacity) if(capacity throw new IllegaLArgumentException"Array initial size error." SuppressWarningsC"unchecked" TC] tempStack-CT new 0bject[capacity] data-tempStack; topOfStack-1; Another constructor takes no parameters and sets the capacity of the data array to the default value. Add a private method called expandArray that will double the size of the array when called. To reduce the amount of code, you can use the copyOf method in java.util.Arrays Add another private method called is ArrayFullO that returns true if the topOfStack exceeds the largest index value in the array Stacklnterface method implementations Add a method called push that will add an element to the top of the stack If the array is full, increase the length of the array by calling the private method, expandArrayO that you wrote to expand the array Increase the top0fStack to point to the new index location and insert the new element into the stack The isEmptyO method returns true if there are no elements in the stack and false otherwise. If the top of the stack is less than 0 then there are no elements currently in the stack. Implement isEmptyO The pop method will remove the last element that was added to the stack. Implement popOby following these guidelines If stack is empty, throw an EmptyStackException. If there are elements in the stack, get the element at the top of the stack (index is topOfstack) and decrement the index of the top of the stack. Set the previous top of the stack to null. Decrement the index of the top of the stack and return the previous top. Add a method called peek() that will return the top of the stack but will not remove it. If the stack is empty, throw an exception. Otherwise, return the element at the top of the stack

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

DB2 11 The Ultimate Database For Cloud Analytics And Mobile

Authors: John Campbell, Chris Crone, Gareth Jones, Surekha Parekh, Jay Yothers

1st Edition

1583474013, 978-1583474013

More Books

Students also viewed these Databases questions

Question

1 understand the meaning of the term cost of capital;

Answered: 1 week ago

Question

BPR always involves automation. Group of answer choices True False

Answered: 1 week ago

Question

1. Does your voice project confidence? Authority?

Answered: 1 week ago