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(); }
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<T> implements QueueADT<T>
{
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<T>
{
public void enqueue(T element);
public T dequeue();
public T first();
public boolean isEmpty();
public int size();
public String toString();
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)