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(); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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 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();
}

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education