Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

public class BArrayQueue implements BoundedQueueInterface {

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 extends QueueInterface { void enqueue(T element) throws QueueOverflowException; }

public interface QueueInterface { T dequeue() throws QueueUnderflowException; boolean isEmpty(); }

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 q1 = new BArrayQueue(5); } }

Complete the implementations of the, so that all the methods have an appropriate implementation 1. a. BArrayQueue enqueue() dequeue() . isEmpty( .isFull() b. UArrayQueue enqueuel) e dequeue isEmpty .enlarge() c. LinkedQueue enqueue) e dequeue isEmpty 2. Include a toString() method for each of the Queue implementations. The toString method should display a simple representation of the elements stored in the queue, from front to rear. Include a size() method for each of the Queue implementations. This method should return the number of elements in the queue (not the maximum capacity) 3. 4. Update the queue tester to include tests for all three queues, including a. isEmpty method - true and false b. isFull method empty and false (for the ArrayQueue) c. enqueue with exception for ArrayQueue d. dequeue - with/without exception for all e. toString - at various points before/after queue and dequeue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Write short notes on Interviews.

Answered: 1 week ago