ICS 211 Homework 7

Queue assignment

In this assignment, you implement two classes of generic queues that support the following methods:

boolean offer(T value);  // returns true if the insertion succeeds
T poll();                // returns the first object, or null if there are none

These queues have two special properties:

  1. any null value given to offer must be discarded rather than added at the end of the queue, and offer must return false.
  2. if the value is the same (as determined by calling value.equals(...)) as any value already in the queue, the value should again not be inserted, and offer must return false.

The first class you implement must use a linked list to implement these queue methods, and the queue grows to whatever size is needed. The second class you implement must use an array to implement this queue class. The size of this array should be 10 and never change.

To implement the first queue you may use the generic node class (presented in the lectures), or your own similar generic node class. The second queue must be implemented using a fixed-size Java array.

You must also build a main method that allows the user to enter strings into the queue, get and print the string at the head of the queue (by entering the command "poll"), insert a null value (command "null"), or exit the program (command "quit"). Any other string enterd by the user should be inserted into the queue by calling the offer method.

By simply changing the name of the class used in the main method, your main method should work with either of your two classes.

For this project you may use and adapt any of the code presented as part of the class, including any code in the book or on the course web site. None of this code will do exactly what is needed for the assignment, but you are welcome to adapt it. If you do, provide a comment clearly indicating the source of your material (except you do not need to do this for the generic Node class), specifying the pages in the book or the URLs from which you have drawn your inspiration or parts of your code.

Sample interaction

Your program could behave as follows, using either implementation (you are also welcome to provide a GUI, if you prefer). The parts your program prints are in this font, the user input is in this font.


poll, null, quit, or a string to insert foo
added element "foo" at the back of the queue
poll, null, quit, or a string to insert poll
got element "foo" from the front of the queue
poll, null, quit, or a string to insert null
failed to add element (null) at the back of the queue
poll, null, quit, or a string to insert bar
added element "bar" at the back of the queue
poll, null, quit, or a string to insert baz
added element "baz" at the back of the queue
poll, null, quit, or a string to insert baz
failed to add element "baz" at the back of the queue
poll, null, quit, or a string to insert bar
failed to add element "bar" at the back of the queue
poll, null, quit, or a string to insert poll
got element "bar" from the front of the queue
poll, null, quit, or a string to insert poll
got element "baz" from the front of the queue
poll, null, quit, or a string to insert quit
done

Turning in the Assignment

Email all the java source code for your assignment to the TA following the instructions posted here. Your source code must include all the parts of each class, even if the instructor or the authors of the textbook originally wrote some of the code.