Question: (1) Write ArrayBoundedQueue.java. QueueInterface.java (pages 221-222) public interface QueueInterface { void enqueue(T element) throws QueueOverflowException1; // Throws QueueOverflowException if this queue is full; // otherwise,

(1) Write ArrayBoundedQueue.java.

QueueInterface.java (pages 221-222)

public interface QueueInterface { void enqueue(T element) throws QueueOverflowException1; // Throws QueueOverflowException if this queue is full; // otherwise, adds element to the rear of this queue. T dequeue() throws QueueUnderflowException; // Throws QueueUnderflowException if this queue is empty; // otherwise, removes front element from this queue and returns it. boolean isFull(); // Returns true if this queue is full; // otherwise, returns false. boolean isEmpty(); // Returns true if this queue is empty; // otherwise, returns false. int size(); // Returns the number of elements in this queue. }

ArrayBoundQueue.java (pages 227-229)

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 the 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]2; rear = DEFCAP - 1; } public ArrayBoundedQueue(int maxSize) { elements = (T[]) new Object[maxSize]3; rear = maxSize - 1; }

QueueUnderflowException.java

QueueOverflowException.java

(2) Test (1) with RepeatStrings.java (Pages 222-223)

import java.util.Scanner; public class RepeatStrings { public static void main(String[] args) { Scanner scan = new Scanner(System.in); QueueInterface stringQueue; stringQueue = new ArrayBoundedQueue(3); String line; for (int i = 1; i <= 3; i++) { System.out.print("Enter a line of text > "); line = scan.nextLine(); stringQueue.enqueue(line); } System.out.println(" Order is: "); while (!stringQueue.isEmpty()) { line = stringQueue.dequeue(); System.out.println(line); } }

(3) Add the following methods to (1), 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!