Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I need help with this code. It's in JAVA for Data Structure. Here's the ArrayBoundedStack code. public class ArrayBoundedStack implements StackInterface { protected final

Hi I need help with this code. It's in JAVA for Data Structure.

image text in transcribed

Here's the ArrayBoundedStack code.

public class ArrayBoundedStack implements StackInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // holds stack elements protected int topIndex = -1; // index of top element in stack public ArrayBoundedStack() { elements = (T[]) new Object[DEFCAP]; } public ArrayBoundedStack(int maxSize) { elements = (T[]) new Object[maxSize]; } public void push(T element) // Throws StackOverflowException if this stack is full, // otherwise places element at the top of this stack. { if (isFull()) throw new StackOverflowException("Push attempted on a full stack."); else { topIndex++; elements[topIndex] = element; } } public void pop() // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else { elements[topIndex] = null; topIndex--; } } public T top() // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. { T topOfStack = null; if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else topOfStack = elements[topIndex]; return topOfStack; } public boolean isEmpty() // Returns true if this stack is empty, otherwise returns false. { return (topIndex == -1); } public boolean isFull() // Returns true if this stack is full, otherwise returns false. { return (topIndex == (elements.length - 1)); } }

StackInterface code:

public interface StackInterface { void push(T element) throws StackOverflowException; // Throws StackOverflowException if this stack is full, // otherwise places element at the top of this stack. void pop() throws StackUnderflowException; // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. T top() throws StackUnderflowException; // Throws StackUnderflowException if this stack is empty, // otherwise returns top element of this stack. boolean isEmpty(); // Returns true if this stack is empty, otherwise returns false. boolean isFull(); // Returns true if this stack is full, otherwise returns false. }
30. Using the ArrayBoundedStack class, create an application EditString that prompts the user for a string and then repeatedly prompts the user for changes to the string, until the user enters an X, indicating the end of changes. Legal change operations are: U-make all letters uppercase L-make all letters lowercase R-reverse the string Cch1ch2 - change all occurrences of ch1 to ch2 Z-undo the most recent change You may assume a "friendly user," that is, the user will not enter anything illegal. When the user is finished the resultant string is printed. For example, if the user enters: Al1 dogs go to heaven U R Z C OA C A t Z the output from the program will be "ALL DAGS GA TA HEAVEN

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 Horse Betting The Road To Absolute Horse Racing 2

Authors: NAKAGAWA,YUKIO

1st Edition

B0CFZN219G, 979-8856410593

More Books

Students also viewed these Databases questions