SWE 619 In Class Exercise Number 2A
September 12, 2017


Consider a simple generic Queue implementation.

public class Queue <E> {

   private List<E> elements;
   private int size;

   public Queue() {   
      this.elements = new ArrayList<E>();
      this.size = 0;
   }

   public void enQueue (E e) {
     elements.add(e);
     size++;
   }

   public E deQueue () {
     if (size == 0) throw new IllegalStateException("Queue.deQueue");
     E result = elements.get(0);
     elements.remove(0);
     size--;
     return result;
   }

   public boolean isEmpty() {
      return size == 0;
   }
}
  1. Rewrite Queue to be immutable. Keep the representation variables elements and size.
  2. Do the right thing with enQueue().
  3. Do the right thing with deQueue().