Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a display method to go inside of the ArrayStack class. Display the content

ID: 3818808 • Letter: W

Question

Write a display method to go inside of the ArrayStack class. Display the contents like this (including the BOTTOM and TOP labels):

BOTTOM element3 element2 element1 TOP

The method header is:

public void display()

Notes:

This method goes inside of the ArrayStack class, so you can directly access the instance data variables of that class.

For full credit, your methods should be efficient- O(n ).

Take advantage of being able to directly access the array.

import java.util.Arrays;
import java.util.EmptyStackException;



public final class ArrayStack<T> implements StackInterface<T> {
   private T[] stack; // Array of stack entries
   private int topIndex; // Index of top entry
   private boolean initialized = false;
   private static final int DEFAULT_CAPACITY = 50;
   private static final int MAX_CAPACITY = 10000;

   public ArrayStack() {
       this(DEFAULT_CAPACITY);
   } // end default constructor

   public ArrayStack(int initialCapacity) {
       checkCapacity(initialCapacity);

       // The cast is safe because the new array contains null entries
       @SuppressWarnings("unchecked")
       T[] tempStack = (T[]) new Object[initialCapacity];
       stack = tempStack;
       topIndex = -1;
       initialized = true;
   } // end constructor

   public void push(T newEntry) {
       checkInitialization();
       ensureCapacity();
       stack[topIndex + 1] = newEntry;
       topIndex++;
   } // end push

   public T peek() {
       checkInitialization();
       if (isEmpty())
           throw new EmptyStackException();
       else
           return stack[topIndex];
   } // end peek

   public T pop() {
       checkInitialization();
       if (isEmpty())
           throw new EmptyStackException();
       else {
           T top = stack[topIndex];
           stack[topIndex] = null;
           topIndex--;
           return top;
       } // end if
   } // end pop

   public boolean isEmpty() {
       return topIndex < 0;
   } // end isEmpty

   public void clear() {
       checkInitialization();

       // Remove references to the objects in the stack,
       // but do not deallocate the array
       while (topIndex > -1) {
           stack[topIndex] = null;
           topIndex--;
       } // end while
           // Assertion: topIndex is -1
   } // end clear

   public void display() {
       // YOUR CODE HERE!
   }
  
   // Throws an exception if this object is not initialized.
   private void checkInitialization() {
       if (!initialized)
           throw new SecurityException(
                   "ArrayStack object is not initialized properly.");
   } // end checkInitialization

   // Throws an exception if the client requests a capacity that is too large.
   private void checkCapacity(int capacity) {
       if (capacity > MAX_CAPACITY)
           throw new IllegalStateException("Attempt to create a stack "
                   + "whose capacity exceeds " + "allowed maximum.");
   } // end checkCapacity

   // Doubles the size of the array stack if it is full
   // Precondition: checkInitialization has been called.
   private void ensureCapacity() {
       if (topIndex >= stack.length - 1) // If array is full, double its size
       {
           int newLength = 2 * stack.length;
           checkCapacity(newLength);
           stack = Arrays.copyOf(stack, newLength);
       } // end if
   } // end ensureCapacity
} // end ArrayStack

Explanation / Answer

   // Paste this method in your code

    public void display() {
       System.out.print("BOTTOM ");
       for (int i=0; i<topIndex; i++) {
           System.out.println(stack[i] + " ");
       }
       System.out.println("TOP");
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote