Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class ArrayBoundedQueue implements QueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array that holds queue elements protected

image text in transcribed

public class ArrayBoundedQueue implements QueueInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // array that holds queue elements protected int numElements = 0; // number of elements in this queue protected int front = 0; // index of front of queue protected int rear; // index of rear of queue

public ArrayBoundedQueue() { elements = (T[]) new Object[DEFCAP]; rear = DEFCAP - 1; }

public ArrayBoundedQueue(int maxSize) { elements = (T[]) new Object[maxSize]; rear = maxSize - 1; }

public void enqueue(T element) // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. { if (isFull()) throw new QueueOverflowException("Enqueue attempted on a full queue."); else { rear = (rear + 1) % elements.length; elements[rear] = element; numElements = numElements + 1; } }

public T dequeue() // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. { if (isEmpty()) throw new QueueUnderflowException("Dequeue attempted on empty queue."); else { T toReturn = elements[front]; elements[front] = null; front = (front + 1) % elements.length; numElements = numElements - 1; return toReturn; } }

public boolean isEmpty() // Returns true if this queue is empty; otherwise, returns false. { return (numElements == 0); }

public boolean isFull() // Returns true if this queue is full; otherwise, returns false. { return (numElements == elements.length); } public int size() // Returns the number of elements in this queue. { return numElements; } }

Add the following methods to the ArrayBoundedQueue class, and create a test driver for each to show that they work correctly. In order to practice your array coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedQueue, not by calling the previously defined public methods of the class. a. String toString () creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each queued element already provides its own reasonable tostring method. b. int space () returns an integer indicating how many empty spaces remain in the queue. c. void remove(int count) removes the front count elements from the queue, and throws QueueUnderflowException if there are less than count elements in the queue. d. boolean swapStart() returns false if there are less than two elements in the queue; otherwise it reverses the order of the front two elements in the queue and returns true. e. boolean swapEnds() returns false if there are less than two elements in the queue; otherwise it swaps the first and last elements of the queue and returns true

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. If yes, then why?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago