(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); } }
(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);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps