Class ArrayQueue<E>

  • All Implemented Interfaces:
    BoundedQueue<E>

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

    The constructor requires time linear in the capacity of the BoundedQueue being created. All other operations require constant time in the worst case.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayQueue​(int maxSize)
      Creates an ArrayQueue whose size and capacity are both equal to the input maxSize, and whose entries are all null, if this is a positive int - throwing an IllegalArgumentException if the input 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 queue.
      void insert​(E x)
      Inserts a new entry onto the queue.
      boolean isEmpty()
      Reports whether the queue is currently empty without changing it.
      E peek()
      Reports the element at the front of the queue without changing it.
      E remove()
      Removes an element from the front of the queue and reports it.
      • Methods inherited from class java.lang.Object

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

      • ArrayQueue

        public ArrayQueue​(int maxSize)
        Creates an ArrayQueue whose size and capacity are both equal to the input maxSize, and whose entries are all null, if this is a positive int - throwing an IllegalArgumentException if the input maxSize is negative, instead.

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

      • insert

        public void insert​(E x)
                    throws QueueFullException
        Description copied from interface: BoundedQueue
        Inserts a new entry onto the queue.
        Specified by:
        insert in interface BoundedQueue<E>
        Parameters:
        x - the object to be inserted onto the queue
        Throws:
        QueueFullException - if the queue is already full

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

        public E peek()
        Description copied from interface: BoundedQueue
        Reports the element at the front of the queue without changing it.
        Specified by:
        peek in interface BoundedQueue<E>
        Returns:
        the element at the front of the queue
      • remove

        public E remove()
        Description copied from interface: BoundedQueue
        Removes an element from the front of the queue and reports it.
        Specified by:
        remove in interface BoundedQueue<E>
        Returns:
        the element removed from the queue
      • isEmpty

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

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

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

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