Solve In Java Write the method code only, I will give you the implementation of ArrayStack Write a method called removeBelow in the class StackApplication
Solve In Java Write the method code only, I will give you the implementation of ArrayStack
Write a method called removeBelow in the class StackApplication that accepts two parameters called elem of type int and an ArrayStack of integers st. The method should remove all the elements of the stack that are below elem from the stack.
Example:
elem: 15
top
Stack (before method call): 20 14 18 16 10
top
Stack (After method call): 20 18 16
Method heading:
public static void removeBelow(int elem, ArrayStack
implementation of ArrayStack
import java.util.NoSuchElementException; /** Implementation of the interface StackInt using an array. The * bottom element of the stack is in location 0 in the array. */ public class ArrayStack
/** * Construct an empty stack with the default initial capacity. */ public ArrayStack() { topOfStack = -1; // Initially empty stack. theData = (E[]) new Object[INITIAL_CAPACITY]; } /** * Construct an empty stack with the initial capacity equal to * parameter cap. * @param cap The initial capacity of the stack */ public ArrayStack(int cap) { topOfStack = -1; // Initially empty stack. if (cap <= 0 ) cap = INITIAL_CAPACITY; theData = (E[]) new Object[cap]; }
/** Copy constructor Construct a new stack as copy of the existing * stack passed as parameter. * @param other The original stack * copy is made in this object */ public ArrayStack (ArrayStack
/** * Insert a new item on top of the stack. * @post The new item is the top item on the stack. * @param obj The item to be inserted * @return The item that was inserted */ @Override public E push(E obj) { if (topOfStack == theData.length - 1) { reallocate(); } topOfStack++; theData[topOfStack] = obj; return obj; }
/** * Remove and return the top item on the stack. * @pre The stack is not empty. * @post The top item on the stack has been removed and * the stack is one item smaller. * @return The top item on the stack * @throws NoSuchElementException, if the stack is empty */ @Override public E pop() { if (isEmpty()) { throw new NoSuchElementException(); } E result = theData[topOfStack]; theData[topOfStack] = null; topOfStack--; return result; }
/** * Return the top item on the stack * Pre: The stack is not empty * Post: The stack remains unchanged * @return The top item on the stack * @throws NoSuchElementException, if the stack is empty */ @Override public E peek() { if (isEmpty()) { throw new NoSuchElementException(); } return theData[topOfStack]; }
/** * Return true if the stack is empty * @return True if the stack is empty */ @Override public boolean isEmpty() { return (topOfStack == -1); }
/** * Method to reallocate the array containing the stack data * @post The size of the data array has been doubled * and all of the data has been copied to the new array */ private void reallocate() { E[] temp = (E[]) new Object[2 * theData.length]; System.arraycopy(theData, 0, temp, 0, theData.length); theData = temp; } } //end ArrayStack
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started