Difference between revisions of "Java - Static Queue Assignment"

From WLCS
Line 18: Line 18:
 
** int [] queue
 
** int [] queue
  
=== Queue Methods ===
+
=== Queue Constructors ===
 
* default constructor: Queue() - sets the queue array size to 5
 
* default constructor: Queue() - sets the queue array size to 5
 
* specific constructor: Queue(int size) - sets the queue array size to size
 
* specific constructor: Queue(int size) - sets the queue array size to size
 +
 +
=== Queue Methods ===
 
* void enqueue(int data) - adds data to the tail of the queue if it is not full
 
* void enqueue(int data) - adds data to the tail of the queue if it is not full
 
* void add(int data) - adds data to the tail of the queue if it is not full
 
* void add(int data) - adds data to the tail of the queue if it is not full

Revision as of 12:06, 2 November 2015

Objectives

  • You will implement a static-sized queue using an array
  • You will implement queue methods (enqueue/add, dequeue/remove, etc.)
  • You will test your static queue in a main() method

Resources

Directions

  • Create a StaticQueue project
    • Use StaticStack as a template -- you will need to create Queue.java and QueueMain.java

Queue Attributes

  • Initialize them to appropriate default values
    • int head
    • int tail
    • int [] queue

Queue Constructors

  • default constructor: Queue() - sets the queue array size to 5
  • specific constructor: Queue(int size) - sets the queue array size to size

Queue Methods

  • void enqueue(int data) - adds data to the tail of the queue if it is not full
  • void add(int data) - adds data to the tail of the queue if it is not full
  • int dequeue() - removes and returns data from the head of the queue if it is non-empty
  • int remove() - removes and returns data from the head of the queue if it is non-empty
  • boolean isFull() - returns true if the queue is full, false otherwise
  • boolean isEmpty() - returns true if the queue is empty, false otherwise
  • int peek() - returns data element at the head (but does not remove it)
  • void print() - prints out the queue from head to tail
  • String toString() - returns the String representation of a queue

Testing Queue

  • Test your queue class to see if it works (Hint: use the Stack main() as an example)

Circular Queue Challenge

  1. What happens if you add and remove a whole bunch of times? i.g. What happens if the head and tail hit the end of the array?
  2. Create a new class named CircularQueue
  3. Copy and paste your static queue code
  4. Add a new attribute named size to keep track of the number of added elements
  5. Modify your the code in the rest of circular queue so that both head and tail are able to wrap around back to the beginning. You will need to use your size attribute in several methods