package edu.gmu; /** This class creates a circular queue backed by an array and then tests it. */ public class TestGmu { private int count = 0; private int back=0; private int front=0; /** How big can our queue be? */ public static final int MAX_QUEUE = 5; /** This is the actual array we're using to hold the data. */ private char [] arr = new char[MAX_QUEUE]; /** Are we in debug mode or not? 1=ON, 2=OFF.*/ private static final int DEBUG = 1; /** Constructor. For now, doesn't need to do anything. */ public TestGmu() { } /** Remove everything from the queue. */ public void clearQueue() { } /** Returns true if the queue is empty. */ public boolean isEmpty() { if (count == 0) return true; else return false; // Works... and is a little nicer! //return (count == 0); // Why doesn't this work? //if (arr[front] == null) return true; //else return false; } /** Returns true if the queue is full. */ public boolean isFull() { return (count == MAX_QUEUE-1); } /** Add the characater c into the queue. * @return true if the char is added to the queue, false otherwise. */ public boolean enqueue(char c) { if(isFull()) return false; arr[back] = c; back = (back+1) % MAX_QUEUE; count++; return true; } /** Return the first element in the array. Throws EmptyQueueException if the queue * is empty */ public char dequeue() { if (isEmpty()) throw new RuntimeException("Empty Queue"); // DEBUG: NEED TO FIX AND THROW CORRECT EXCEPTION char temp = arr[front]; arr[front] = ' '; front = (front+1) % MAX_QUEUE; count--; return temp; } /** Debug method to print the queue in order! */ private void printQueue() { for (int i=0; i