Question
JAVA - Queues public class BArrayQueue implements BoundedQueueInterface { protected int front; protected int rear; protected int numElements; protected T[] queue; protected static final int
JAVA - Queues
public class BArrayQueue
protected int front; protected int rear; protected int numElements; protected T[] queue; protected static final int DEFCAP = 10;
public BArrayQueue() { this(DEFCAP); }
public BArrayQueue(int capacity) { numElements = 0; front = -1; rear = -1; queue = (T[]) new Object[capacity]; }
@Override public T dequeue() throws QueueUnderflowException { // TODO Auto-generated method stub if (isEmpty()) throw new QueueUnderflowException("Attempted dequeue on an empty queue"); else { T retData = queue[front]; queue[front] = null; front = (front + 1) % queue.length; numElements--; if (isEmpty()) { front = -1; rear = -1; } return retData; } }
@Override public boolean isEmpty() { // TODO Auto-generated method stub return (numElements == 0); }
@Override public void enqueue(T element) { // TODO Auto-generated method stub if (isFull()) throw new QueueOverflowException("Attempted queue on a full queue"); else { numElements++; rear = (rear+1) % queue.length; queue[rear] = element;
if (numElements == 1) front = rear; } }
public boolean isFull() { return (numElements == queue.length); }
}
public interface BoundedQueueInterface
public interface QueueInterface
public class QueueOverflowException extends RuntimeException { public QueueOverflowException() { super(); } public QueueOverflowException(String message) { super(message); } }
public class QueueUnderflowException extends RuntimeException { public QueueUnderflowException() { super(); } public QueueUnderflowException(String message) { super(message); } }
public class QueueTester {
public static void main(String[] args) {
BArrayQueue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started