IN JAVA First, Implement the method public void display() which displays the entries in a stack starting from the top. If the stack is empty, print “The stack is empty”. Add the method to ArrrayStack2.java. You do not need to modify StackInterface.java. Then, Implement a method

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

IN JAVA

First, Implement the method

public void display()

which displays the entries in a stack starting from the top. If the stack is empty, print “The stack is empty”.

Add the method to ArrrayStack2.java. You do not need to modify StackInterface.java.

Then, Implement a method

 

public int remove(int n)

 

The method removes the n top most entries for a stack . If the stack contains less than n items, the stack becomes empty. The method returns the number of items removed.

Add the method to ArrrayStack2.java. You do not need to modify StackInterface.java.

Then, Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations.

Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows:

 

The first new method checks whether we should reduce the size of the array:

private boolean isTooBig()

 

This method returns true if the number of entries in the stack is less than half the size of the array and the size of the array is greater than 20.

 

The second new method creates a new array that is three quarters the size of the current array and then copies the objects in the bag to the new array:

private void reduceArray()

 

Implement each of these two methods, and then use them in the definition of pop()

Expert Solution
Step 1

public class ArrayStack2<T> implements StackInterface<T> {

  private T[] stack;

  private int topIndex;

  private static final int DEFAULT_CAPACITY = 50;

 

  public ArrayStack2() {

    this(DEFAULT_CAPACITY);

  }

 

  public ArrayStack2(int initialCapacity) {

    @SuppressWarnings("unchecked")

    T[] tempStack = (T[]) new Object[initialCapacity];

    stack = tempStack;

    topIndex = -1;

  }

 

  public void display() {

    if (isEmpty()) {

      System.out.println("The stack is empty");

    } else {

      System.out.println("The entries in the stack are:");

      for (int i = topIndex; i >= 0; i--) {

        System.out.println(stack[i]);

      }

    }

  }

 

  public int remove(int n) {

    int itemsRemoved = 0;

    if (n > topIndex + 1) {

      itemsRemoved = topIndex + 1;

      topIndex = -1;

    } else {

      itemsRemoved = n;

      topIndex -= n;

    }

    return itemsRemoved;

  }

 

  // ... Other existing methods from the ArrayStack2 class ...

  

  private boolean isTooBig() {

    return topIndex < stack.length / 2 && stack.length > 20;

  }

 

  private void reduceArray() {

    T[] newStack = (T[]) new Object[stack.length * 3 / 4];

    for (int i = 0; i <= topIndex; i++) {

      newStack[i] = stack[i];

    }

    stack = newStack;

  }

  

  public T pop() {

    T top = null;

    if (!isEmpty()) {

      top = stack[topIndex];

      stack[topIndex] = null;

      topIndex--;

      if (isTooBig()) {

        reduceArray();

      }

    }

    return top;

  }

}

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning