Question: Complete the implementation of the isEmpty(), first(), size() and toString() methods. Also create a main() method class to write two or more test scenarios that

Complete the implementation of the isEmpty(), first(), size() and toString() methods. Also create a main() method class to write two or more test scenarios that will demonstrate the queue operation of all public methods including a scenario that adds values beyond the original capacity of the queue. Instantiating each queue with a relatively small capacity is recommended. Demonstrate that you can instantiate CircularArrayQueue classes with different data types, enqueue several items, dequeue several items, display the queue contents after each operation, attempt dequeueing from an empty queue.

Please add comments so I can understand your code.

CircularArrayQueue.java

public class CircularArrayQueue implements QueueADT { private final static int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = (T[]) (new Object[initialCapacity]); } public CircularArrayQueue() { this(DEFAULT_CAPACITY); } public void enqueue(T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } private void expandCapacity() { T[] larger = (T[]) (new Object[queue.length *2]); for (int scan = 0; scan < count; scan++) { larger[scan] = queue[front]; front = (front + 1) % queue.length; } front = 0; rear = count; queue = larger; } 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; } public T first() throws EmptyCollectionException { // To be completed } public boolean isEmpty() { // To be completed } public int size() { // To be completed } public String toString() { // To be completed } }

EmptyCollectionException.java

public class EmptyCollectionException extends RuntimeException { public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } }

QueueADT.java

public interface QueueADT { public void enqueue(T element);

public T dequeue();

public T first(); public boolean isEmpty();

public int size();

public String toString(); }

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!