Question: Java In this assignment, we implement a variant of BoundedArrayStack. Your class should be named as RecentArrayStack, and it should be a subclass of BoundedArrayStack.

Java

In this assignment, we implement a variant of BoundedArrayStack. Your class should be named as RecentArrayStack, and it should be a subclass of BoundedArrayStack. The only difference between the RecentArrayStack and BoundedArrayStack is the push operation. There will be no overflow exception in the RecentArrayStack, instead, the oldest element in the stack will be overwritten by the new element.

You should also rewrite other methods as needed to reflect the changes.

Given Codes:

BoundedArrayStack

package chapter14;

import java.util.EmptyStackException;

/** * The array based implementation of stack. */ public class BoundedArrayStack implements StackInterface { protected static final int DEFAULT_CAPACITY = 4; protected T[] stack; protected int topIndex;

public BoundedArrayStack() { this(DEFAULT_CAPACITY); }

public BoundedArrayStack(int capacity) { topIndex = -1; stack = (T[]) new Object[capacity]; }

@Override public void push(T element) throws StackOverflowException { if (!isFull()) { topIndex++; stack[topIndex] = element; } else { throw new StackOverflowException("Stack is full"); } }

@Override public T pop() throws EmptyStackException { if (!isEmpty()) { T res = stack[topIndex--]; // null the entry stack[topIndex + 1] = null;

return res; } else { throw new EmptyStackException(); } }

@Override public T top() throws EmptyStackException { if (!isEmpty()) { return stack[topIndex]; } else throw new EmptyStackException(); }

@Override public boolean isEmpty() { return topIndex == -1; }

@Override public boolean isFull() { return topIndex == stack.length - 1; } public int size() { return topIndex + 1; } }

StackInterface

package chapter14;

import java.util.EmptyStackException;

/** * Represents a last-in-first-out (LIFO) stack of objects. * */ public interface StackInterface { /** * Push the element to the stack. * @param element the element to be pushed onto the stack. */ void push(T element);

/** * Removes the top element and returns it if the stack is not empty. * @return the top if the stack is not empty. * @throws EmptyStackException when the stack is empty. */ T pop() throws EmptyStackException;

/** * Looks at the object at the top of this stack without removing it. * @return the top if the stack is not empty. * @throws EmptyStackException when the stack is empty. */ T top() throws EmptyStackException;

/** * @return true if the stack is empty, false otherwise. */ boolean isEmpty();

/** * Optional * @return true if the stack is full, false otherwise. */ default boolean isFull() { return false; } } StackOverflowException

package chapter14;

public class StackOverflowException extends RuntimeException {

public StackOverflowException() { super(); // TODO Auto-generated constructor stub }

public StackOverflowException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { super(arg0, arg1, arg2, arg3); // TODO Auto-generated constructor stub }

public StackOverflowException(String arg0, Throwable arg1) { super(arg0, arg1); // TODO Auto-generated constructor stub }

public StackOverflowException(String arg0) { super(arg0); // TODO Auto-generated constructor stub }

public StackOverflowException(Throwable arg0) { super(arg0); // TODO Auto-generated constructor stub }

} Sample input

3 A A A B A C A D A E F E D F E D E D E S Q 

sample output

Size:1 Size:2 Size:3 Size:3 Size:3 Full: true Empty: false Popped: E Size:2 Full: false Empty: false Popped: D Size:1 Empty: false Popped: C Size:0 Empty: true

More info about what ?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!