Difference between revisions of "Java - Static Queue Assignment"

From WLCS
m (Protected "Java - Static Queue Assignment" ([edit=sysop] (indefinite) [move=sysop] (indefinite)) [cascading])
Line 35: Line 35:
 
=== Testing Queue ===
 
=== Testing Queue ===
 
* Test your queue class to see if it works (Hint: use the Stack main() as an example)
 
* Test your queue class to see if it works (Hint: use the Stack main() as an example)
 +
* Test to make sure special cases work:
 +
*# What happens to the head when you enqueue/add to an empty queue? 
 +
*# What happens to the tail when you dequeue/remove the last item in the queue?
  
 
== Circular Queue Challenge ==
 
== Circular Queue Challenge ==

Revision as of 11:24, 28 October 2016

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)
  • Test to make sure special cases work:
    1. What happens to the head when you enqueue/add to an empty queue?
    2. What happens to the tail when you dequeue/remove the last item in the queue?

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