Package project2

Class ResizableArrayStack<T>

java.lang.Object
project2.ResizableArrayStack<T>
Type Parameters:
T - the type of the elements in the stack
All Implemented Interfaces:
StackInterface<T>

public final class ResizableArrayStack<T> extends Object implements StackInterface<T>
A class of stacks whose entries are stored in an array. The array is expanded when the stack is full. Implements the StackInterface.
Version:
1.0
Author:
Lindsay Kislingbury
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty stack whose initial capacity is 50.
    ResizableArrayStack(int initialCapacity)
    Creates an empty stack having a given initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all entries from this stack.
    boolean
    Detects whether this stack is empty.
    Retrieves this stack's top entry.
    pop()
    Removes and returns this stack's top entry.
    void
    push(T newEntry)
    Adds a new entry to the top of this stack.
    int
    Returns the number of elements in the stack.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResizableArrayStack

      public ResizableArrayStack()
      Creates an empty stack whose initial capacity is 50.
    • ResizableArrayStack

      public ResizableArrayStack(int initialCapacity)
      Creates an empty stack having a given initial capacity. Throws an IllegalStateException if the initial capacity is outside the range of 0 to MAX_CAPACITY.
      Parameters:
      initialCapacity - The integer capacity desired.
  • Method Details

    • size

      public int size()
      Returns the number of elements in the stack.
      Returns:
      The number of elements in the stack.
    • push

      public void push(T newEntry)
      Adds a new entry to the top of this stack.
      Specified by:
      push in interface StackInterface<T>
      Parameters:
      newEntry - An object to be added to the stack.
      Throws:
      IllegalArgumentException - if the input is null.
      SecurityException - if the object is corrupt.
    • pop

      public T pop()
      Removes and returns this stack's top entry.
      Specified by:
      pop in interface StackInterface<T>
      Returns:
      The object at the top of the stack.
      Throws:
      EmptyStackException - if the stack is empty
      SecurityException - if the object is corrupt.
    • peek

      public T peek()
      Retrieves this stack's top entry.
      Specified by:
      peek in interface StackInterface<T>
      Returns:
      The object at the top of the stack.
      Throws:
      EmptyStackException - if the stack is empty
      SecurityException - if the object is corrupt.
    • isEmpty

      public boolean isEmpty()
      Detects whether this stack is empty.
      Specified by:
      isEmpty in interface StackInterface<T>
      Returns:
      True if the stack is empty.
    • clear

      public void clear()
      Removes all entries from this stack.
      Specified by:
      clear in interface StackInterface<T>
      Throws:
      SecurityException - if the object is corrupt.