Hi, I've been given an assignment to develop a class that deals with a simple shuffling array type queue with the following important methods:
enqueue: adds an object to the rear of the queue.
dequeue: removes and returns an object from the front of the queue.
peek: returns the object at the front of the queue without removing it.
I've got most of it implemented and working but I'm having trouble getting the dequeue method working according to the test code I've been given (which I will include below).
Here is my code:
public class DSAQueue { public static final int DEFAULT_CAPACITY = 100; private Object[] queue; private int count; private int front; private int rear; public DSAQueue () { queue = new Object[DEFAULT_CAPACITY]; count = 0; front = 0; rear = 0; } public DSAQueue (int maxCapacity) { queue = new Object[maxCapacity]; count = 0; front = 0; rear = 0; } public int getCount() { return count; } public boolean isEmpty () { return (count == 0); } public boolean isFull () { return (count == queue.length); } public void enqueue (Object value) { if (isFull()) { throw new IllegalStateException("Object array is full"); } else { //System.out.println("rear is: " + rear); //System.out.println("front is: " + front); if(rear == 0) { queue[rear] = value; rear += 1; count += 1; } else { queue[rear += 1] = value; count += 1; } } } public Object dequeue () { Object frontVal; if (isEmpty()) { throw new IllegalStateException("Object array is empty"); } else { frontVal = queue[front]; front += 1; count -= 1; } return frontVal; } public Object peek () { Object frontVal; if (isEmpty()) { throw new IllegalStateException("Object array is empty"); } frontVal = queue[front]; return frontVal; } }
And here is the test code:
import java.io.*; import io.*; public class UnitTestDSAQueueArray { public static void main(String args[]) { int ii; int iNumPassed; int iNumTests; DSAQueue q; Ore ironOre; OrePile orePile; // Assume Ore works... ironOre = new Ore(Ore.ORETYPE_IRON, "t"); q = new DSAQueue(); iNumTests = 0; iNumPassed = 0; System.out.println("\n"); System.out.println("Testing Normal Conditions - Constructor"); System.out.println("======================================="); try { iNumTests++; System.out.print("Testing (count=0): "); if (q.getCount() != 0) throw new IllegalArgumentException("Count must equal zero."); System.out.println("passed"); iNumPassed++; } catch(Exception e) { System.out.println("FAILED"); } try { iNumTests++; System.out.print("Testing dequeue(): "); if (q.dequeue() != null) System.out.println("FAILED"); } catch(Exception e) { iNumPassed++; System.out.println("passed"); } System.out.println("\n"); System.out.println("Testing Normal Conditions - Methods"); System.out.println("==================================="); try { iNumTests++; System.out.println("Testing enqueue(): "); for (ii = 1; ii <= 10; ii++) { // Create queue of iron ore piles. q.enqueue(new OrePile(ironOre, ii, 50)); System.out.println("Front of queue: " + ii); } iNumPassed++; System.out.println("Testing enqueue(): passed"); } catch (Exception e) { System.out.println("FAILED"); } try { iNumTests++; System.out.print("Testing peek(): "); orePile = (OrePile)q.peek(); if (orePile.getWeight() != 1) throw new IllegalStateException("Not looking at first ore pile."); iNumPassed++; System.out.println("passed"); } catch (Exception e) { System.out.println("FAILED"); } try { iNumTests++; System.out.println("Testing dequeue(): "); for (ii = 1; ii <= 10; ii++) { orePile = (OrePile)q.dequeue(); // Verify that queue was OK. if ((int)orePile.getWeight() != ii) { throw new IllegalStateException("Queue testing failed at index " + ii); } System.out.println("Front of queue: " + ii); } iNumPassed++; System.out.println("Testing dequeue(): passed"); } catch (Exception e) { System.out.println("FAILED"); } try { iNumTests++; System.out.print("Testing isEmpty(): "); if (q.isEmpty() == false) throw new IllegalStateException("Queue is empty."); iNumPassed++; System.out.println("passed"); } catch (Exception e) { System.out.println("FAILED"); } try { iNumTests++; System.out.print("Testing isFull(): "); for (ii = 1; ii <= 100; ii++) q.enqueue(new OrePile(ironOre, ii, 50)); if (q.isFull() == false) throw new IllegalStateException("Queue is full."); System.out.println("passed"); iNumPassed++; } catch (Exception e) { System.out.println("FAILED"); } System.out.println("\n"); System.out.println("Testing Error Conditions - Methods"); System.out.println("=================================="); try { iNumTests++; System.out.print("Testing enqueue() for full queue: "); // Enqueue another item onto full queue (see previous test) - should fail. q.enqueue(new OrePile(ironOre, 101, 50)); System.out.println("FAILED"); } catch(Exception e) { iNumPassed++; System.out.println("passed"); } try { iNumTests++; System.out.print("Testing dequeue() for empty queue: "); // First dequeue all items from queue. for (ii = 1; ii <= 100; ii++) orePile = (OrePile)q.dequeue(); // Now try to dequeue a non-existing item. if (q.dequeue() != null) System.out.println("FAILED"); } catch(Exception e) { iNumPassed++; System.out.println("passed"); } try { iNumTests++; System.out.print("Testing peek() for empty queue: "); orePile = (OrePile)q.peek(); System.out.println("FAILED"); } catch (Exception e) { iNumPassed++; System.out.println("passed"); } // Ensure that queue coded to use Object rather than OrePile. try { iNumTests++; System.out.print("Testing Object queue: "); q = new DSAQueue(); for (ii = 1; ii <= 10; ii++) q.enqueue("string test"); System.out.println("passed"); iNumPassed++; } catch (Exception e) { System.out.println("FAILED"); } System.out.println("\n"); System.out.println("Number PASSED: " + iNumPassed + "/" + iNumTests + " (" + (int)(100.0*(double)iNumPassed/(double)iNumTests) + "%)"); } }
Any help you can provide here would be much appreciated, thanks!