Class ArrayStack<E>

  • All Implemented Interfaces:
    BoundedStack<E>

    public class ArrayStack<E>
    extends java.lang.Object
    implements BoundedStack<E>
    Provides an implementation of a BoundedStack using a BoundedArray.

    The constructor requires time linear in the capacity of the BoundedStack being created (because this time is required to initialize the BoundedArray being used). All other operations require constant time in the worst case.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayStack​(int maxSize)
      Creates an initially empty bounded stack whose capacity is the input maxSize, if this is a positive int - throwing an IllegalArgumentException if maxSize is negative, instead.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int capacity()
      Reports the capacity of this bounded stack.
      boolean isEmpty()
      Reports whether the stack is currently empty without changing it.
      E peek()
      Reports the top element of the stack without changing it.
      E pop()
      Removes an element from the top of the stack and reports it.
      void push​(E x)
      Pushes a new entry onto the stack.
      • Methods inherited from class java.lang.Object

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

      • ArrayStack

        public ArrayStack​(int maxSize)
        Creates an initially empty bounded stack whose capacity is the input maxSize, if this is a positive int - throwing an IllegalArgumentException if maxSize is negative, instead.

        Parameters:
        maxSize - the capacity of the BoundedStack to be created
    • Method Detail

      • push

        public void push​(E x)
                  throws StackFullException
        Description copied from interface: BoundedStack
        Pushes a new entry onto the stack.
        Specified by:
        push in interface BoundedStack<E>
        Parameters:
        x - the object to be pushed onto the stack
        Throws:
        StackFullException - if the Stack is already full

        Precondition:
        1. The BoundedStack Invariant is satisfied.
        2. An element x of type E has been given as input.
        Postcondition:
        1. The BoundedStack Invariant is satisfied.
        2. If the number of objects presently on this bounded stack was less than the bounded stack's capacity, then the input object x has been pushed onto the top of the stack (which is otherwise unchanged).
        3. If the number of objects presently on the bounded stack was equal to the bounded stack's capacity, then a StackFullException is thrown and the stack is not changed.
      • peek

        public E peek()
        Description copied from interface: BoundedStack
        Reports the top element of the stack without changing it.
        Specified by:
        peek in interface BoundedStack<E>
        Returns:
        the element at the top of the stack
      • pop

        public E pop()
        Description copied from interface: BoundedStack
        Removes an element from the top of the stack and reports it.
        Specified by:
        pop in interface BoundedStack<E>
        Returns:
        the element removed from the stack
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: BoundedStack
        Reports whether the stack is currently empty without changing it.
        Specified by:
        isEmpty in interface BoundedStack<E>
        Returns:
        true if the stack is empty, and false otherwise

        Precondition:
        1. The BoundedStack Invariant is satisfied.
        Postcondition:
        1. The BoundedStack Invariant is satisfied.
        2. The bounded stack is not changed.
        3. If the bounded stack was empty before this operation then “true” is returned as output.
        4. If the bounded stack was not empty before this operation then “false” is returned as output.
      • capacity

        public int capacity()
        Description copied from interface: BoundedStack
        Reports the capacity of this bounded stack.
        Specified by:
        capacity in interface BoundedStack<E>
        Returns:
        the capacity of this BoundedStack

        Precondition:
        1. The BoundedStack Invariant is satisfied.
        Postcondition:
        1. The BoundedStack Invariant is satisfied.
        2. The bounded stack is not changed.
        3. The capacity of this bounded stack is returned as output.