(JAVA) Data Structures - Implementing a Queue using a Circular Array I've added the QueueOverflowException class, but I'm having trouble with the ArrayQueue Class. If you can do so, can you please add comments to every class and method in just the ArrayQueue class.   ****THIS IS THE CLASS THAT NEEDS ADDITIONAL CODE.***** public class ArrayQueue implements QueueInterface {     public static final int MAX = 10;     private T elements[];     private int front;     private int rear;     public ArrayQueue()     {         //awkward syntax!         elements = (T[]) new Object[MAX];         front = MAX - 1;         rear = MAX - 1;     }     //implement your methods here please } ***THE FOLLOWING CLASSES ARE GIVEN AND NEEDED TO COMPLETE THE PROGRAM**** public class Tester {     public static void main()     {         QueueInterface q = new ArrayQueue();         for (Character ch = 'A'; ch < 'F'; ++ch)             q.insert(ch);         while (!q.isEmpty())             System.out.println(q.remove());         //deliberate underflow         try {             System.out.println(q.remove());         }         catch (QueueUnderflowException underflow) {             System.out.println("Exception: " + underflow.getMessage());             System.err.println("Exception: " + underflow.getMessage());             //allow program to continue after this             //System.exit(1);         }         //deliberate overflow         try {             for (Character ch = 'A'; ch < 'Z'; ++ch)                 q.insert(ch);         }         catch (QueueOverflowException overflow) {             System.out.println("Exception: " + overflow.getMessage());             System.err.println("Exception: " + overflow.getMessage());             System.exit(2);         }         System.out.println("done");     } } ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public class QueueUnderflowException extends RuntimeException {     public QueueUnderflowException()     {         super();       }     public QueueUnderflowException(String message)     {         super(message);       } } ------------------------------------------------------------------------------------------------------------------------------------------------------------- public class LinkedQueue implements QueueInterface {     private Item front;     private Item rear;     public LinkedQueue()     {         front = null;         rear = null;     }     public void insert(T element)     {         Item item = new Item(element);         //queue is empty         if (isEmpty()) {             front = item;             rear = item;         }         //queue is not empty         else {             rear.next = item;             rear = item;         }     }     public T remove() throws QueueUnderflowException     {         if (isEmpty())             throw new QueueUnderflowException("Remove attempted on empty queue");         else {             T info = front.info;             front = front.next;             return info;         }     }     public Boolean isEmpty()     {         return front == null;     } } ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- public interface QueueInterface {     void insert(T element);     T remove() throws QueueUnderflowException;     Boolean isEmpty(); } ---------------------------------------------------------------------------------------------------------------------------------------------------------------     public class Item {     protected T info;     protected Item next;     public Item()     {         info = null;         next = null;     }     public Item(T info)     {         this.info = info;         next = null;     } } --------------------------------------------------------------------------------------------------------------------------------------------------------------- public class QueueOverflowException extends RuntimeException { public QueueOverflowException() { super(); } public QueueOverflowException(String message) { super(message); } }

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

(JAVA) Data Structures - Implementing a Queue using a Circular Array

I've added the QueueOverflowException class, but I'm having trouble with the ArrayQueue Class. If you can do so, can you please add comments to every class and method in just the ArrayQueue class.

 

****THIS IS THE CLASS THAT NEEDS ADDITIONAL CODE.*****

public class ArrayQueue implements QueueInterface
{
    public static final int MAX = 10;

    private T elements[];
    private int front;
    private int rear;

    public ArrayQueue()
    {
        //awkward syntax!
        elements = (T[]) new Object[MAX];
        front = MAX - 1;
        rear = MAX - 1;
    }

    //implement your methods here please
}

***THE FOLLOWING CLASSES ARE GIVEN AND NEEDED TO COMPLETE THE PROGRAM****

public class Tester
{
    public static void main()
    {
        QueueInterface q = new ArrayQueue();

        for (Character ch = 'A'; ch < 'F'; ++ch)
            q.insert(ch);

        while (!q.isEmpty())
            System.out.println(q.remove());

        //deliberate underflow
        try {
            System.out.println(q.remove());
        }
        catch (QueueUnderflowException underflow) {
            System.out.println("Exception: " + underflow.getMessage());
            System.err.println("Exception: " + underflow.getMessage());
            //allow program to continue after this
            //System.exit(1);
        }

        //deliberate overflow
        try {
            for (Character ch = 'A'; ch < 'Z'; ++ch)
                q.insert(ch);
        }
        catch (QueueOverflowException overflow) {
            System.out.println("Exception: " + overflow.getMessage());
            System.err.println("Exception: " + overflow.getMessage());
            System.exit(2);
        }

        System.out.println("done");
    }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class QueueUnderflowException extends RuntimeException
{
    public QueueUnderflowException()
    {
        super();  
    }

    public QueueUnderflowException(String message)
    {
        super(message);  
    }
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------

public class LinkedQueue implements QueueInterface
{
    private Item front;
    private Item rear;

    public LinkedQueue()
    {
        front = null;
        rear = null;
    }

    public void insert(T element)
    {
        Item item = new Item(element);

        //queue is empty
        if (isEmpty()) {
            front = item;
            rear = item;
        }
        //queue is not empty
        else {
            rear.next = item;
            rear = item;
        }
    }

    public T remove() throws QueueUnderflowException
    {
        if (isEmpty())
            throw new QueueUnderflowException("Remove attempted on empty queue");
        else {
            T info = front.info;
            front = front.next;
            return info;
        }
    }

    public Boolean isEmpty()
    {
        return front == null;
    }
}


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

public interface QueueInterface
{
    void insert(T element);

    T remove() throws QueueUnderflowException;

    Boolean isEmpty();
}


---------------------------------------------------------------------------------------------------------------------------------------------------------------

    public class Item
{
    protected T info;
    protected Item next;

    public Item()
    {
        info = null;
        next = null;
    }

    public Item(T info)
    {
        this.info = info;
        next = null;
    }
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------

public class QueueOverflowException extends RuntimeException {

public QueueOverflowException() {
super();
}

public QueueOverflowException(String message) {
super(message);
}

}

Purpose
Purpose is to implement a queue using a circular array.
Implement the ArrayQueue class
In the 'Queues' lecture, review the 'Introduce next lab' section. See here that we can use a circular array to implement the queue data structure. You must write a class named ArrayQueue that does this.
ArrayQueue will be a generic class, that implements our generic Queuelnterface interface. This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the
LinkedQueue class covered during the lecture.
Many classes are given to you
Download and unzip the Circular array project from Canvas, 'Queues' module, Example programs. Open the project in BlueJ and see Tester:main(). This shows the methods you have to implement. You must
write this code in the ArrayQueue class, that implements the circular array. Other than ArrayQueue, the code provided must not be altered in any way.
Hints
Note that the array implementation does not require our Item class. Instead the array contains elements of the generic data type T.
The syntax in the ArrayQueue constructor to create an array of the generic data type T is awkward, so has been given to you. See how the code creates an array of Object, which is then typecast to data type
T. Cool!
Queue overflow was not a problem for our previous linked list implementation, but is obviously a problem for an array implementation. So you will have to add a new QueueOverflowException class.
Run the program to test your completed ArrayQueue class. When correct, save the output as an output.txt text file, saved in the BlueJ project folder.
Transcribed Image Text:Purpose Purpose is to implement a queue using a circular array. Implement the ArrayQueue class In the 'Queues' lecture, review the 'Introduce next lab' section. See here that we can use a circular array to implement the queue data structure. You must write a class named ArrayQueue that does this. ArrayQueue will be a generic class, that implements our generic Queuelnterface interface. This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture. Many classes are given to you Download and unzip the Circular array project from Canvas, 'Queues' module, Example programs. Open the project in BlueJ and see Tester:main(). This shows the methods you have to implement. You must write this code in the ArrayQueue class, that implements the circular array. Other than ArrayQueue, the code provided must not be altered in any way. Hints Note that the array implementation does not require our Item class. Instead the array contains elements of the generic data type T. The syntax in the ArrayQueue constructor to create an array of the generic data type T is awkward, so has been given to you. See how the code creates an array of Object, which is then typecast to data type T. Cool! Queue overflow was not a problem for our previous linked list implementation, but is obviously a problem for an array implementation. So you will have to add a new QueueOverflowException class. Run the program to test your completed ArrayQueue class. When correct, save the output as an output.txt text file, saved in the BlueJ project folder.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
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
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