Assignment 4: Guide – SWE 619 – ALL – Spring 2010

 

Goal: Data Abstraction / Mutability.

 

Basic Assignment

 

Rewrite the generic Queue class below so that it is immutable. You should keep the same representation.

 

/**

  * Generic Queue example

  * Mutable Version, without specifications

  * SWE 619, Fall 2009

  * @author Paul Ammann

  * September 9, 2009

  */

 

import java.util.*;

 

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;

   }

 

  public static void main(String[] args) {

     // Simple exercise to enQueue/deQueue cmd line args

     // Usage:  java Queue item1 item2 item3 ...

     Queue <String> q = new Queue <String>();

     for (String arg : args)

        q.enQueue(arg);

     while (!q.isEmpty() )

        System.out.println(q.deQueue().toUpperCase());

 

  }

 

}

 

Provide an overview, method signatures, method specifications, and the methods themselves. You do not need to provide the abstraction function or representation invariant for this exercise.

Note that you will need to split one of the mutators into an observer and a producer.