Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Added methods NEED to extend ArrayBoundedStack, not added to the actual class. Test Driver to show Extended methods work correctly. package ch02.stacks; public class ArrayBoundedStack

image text in transcribed

Added methods NEED to extend ArrayBoundedStack, not added to the actual class. Test Driver to show Extended methods work correctly.

package ch02.stacks; 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)); } }

--------------------------------------------------------------------------------------------------------------------------------------

package ch02.stacks; 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. }

---------------------------------- Extended.java---------------------------------

import ch02.stacks.*;

public class Extended extends ArrayBoundedStack { //Add methods a through e into this class }

Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related cod- ing skills, code each of these methods by accessing the internal variables of the Ar- rayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()-creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method. b. int size()-returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method. void popSome (int count)-removes the top count elements from the stack; throws StackUnderflowException if there are less than count ele- ments on the stack. d. boolean gwapStart()-if there are less than two elements on the stack re- turns false; otherwise it reverses the order of the top two elements on the stack and returns true. e. I poptop()-the "classic" pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element 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

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions

Question

Why is a mission statement important to an organization?

Answered: 1 week ago

Question

=+12.2. Suppose that A 221, A( A) > 0, and 0 Answered: 1 week ago

Answered: 1 week ago

Question

1. Outline the listening process and styles of listening

Answered: 1 week ago

Question

4. Explain key barriers to competent intercultural communication

Answered: 1 week ago