Question: USING JAVA /* This class contains an incomplete circular array implementation of QueueADT. Your task is to complete the methods that have no implementation. */

USING JAVA

/* This class contains an incomplete circular array implementation of QueueADT.

Your task is to complete the methods that have no implementation.

*/

/**

* CircularArrayQueue represents an array implementation of a queue in

* which the indexes for the front and rear of the queue circle back to 0

* when they reach the end of the array.

*

* @author Lewis and Chase

* @version 4.0

*/

public class CircularArrayQueue implements QueueADT

{

private final static int DEFAULT_CAPACITY = 100;

private int front, rear, count;

private T[] queue;

/**

* Creates an empty queue using the specified capacity.

* @param initialCapacity the initial size of the circular array queue

*/

public CircularArrayQueue (int initialCapacity)

{

front = rear = count = 0;

queue = (T[]) (new Object[initialCapacity]);

}

/**

* Creates an empty queue using the default capacity.

*/

public CircularArrayQueue()

{

// Complete this method.

}

/**

* Adds the specified element to the rear of this queue, expanding

* the capacity of the queue array if necessary.

* @param element the element to add to the rear of the queue

*/

public void enqueue(T element)

{

if (size() == queue.length)

expandCapacity();

queue[rear] = element;

rear = (rear+1) % queue.length;

count++;

}

/**

* Creates a new array to store the contents of this queue with

* twice the capacity of the old one.

*/

private void expandCapacity()

{

// Complete this method.

}

/**

* Removes the element at the front of this queue and returns a

* reference to it.

* @return the element removed from the front of the queue

* @throws EmptyCollectionException if the queue is empty

*/

public T dequeue() throws EmptyCollectionException

{

if (isEmpty())

throw new EmptyCollectionException("queue");

T result = queue[front];

queue[front] = null;

front = (front+1) % queue.length;

count--;

return result;

}

/**

* Returns a reference to the element at the front of this queue.

* The element is not removed from the queue.

* @return the first element in the queue

* @throws EmptyCollectionException if the queue is empty

*/

public T first() throws EmptyCollectionException

{

// Complete this method.

}

/**

* Returns true if this queue is empty and false otherwise.

* @return true if this queue is empty

*/

public boolean isEmpty()

{

// Complete this method.

}

/**

* Returns the number of elements currently in this queue.

* @return the size of the queue

*/

public int size()

{

// Complete this method.

}

/**

* Returns a string representation of this queue.

* @return the string representation of the queue

*/

public String toString()

{

// Complete this method.

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!