Question: Description: In this lab you will augment the basic functionality of the ArrayBoundedQueue with five new methods. Once you have imported the files for the

Description: In this lab you will augment the basic functionality of the ArrayBoundedQueue with five new methods. Once you have imported the files for the lab, you will add the following methods to the ArrayBoundedQueue class file. Use the included test class (or your own test driver) to verify the methods are working. To improve your array processing skills, you may NOT use any of the existing public methods to implement the following methods:

1. public String toString() - creates and returns a string that correctly represents the current data in the queue. You may assume that the objects stored in the queue already have their own toString() method.

2. public int space() - returns an integer indicating how many empty spaces remain in the queue. Once you are finished, submit ONLY the modied file ArrayBoundedQueue.java.

ArrayBoundedQueue

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; } }

Queue Interface

public interface QueueInterface { void enqueue(T element) throws QueueOverflowException; // 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.

Test Driver Class

import java.util.*;

public class ITDArrayBoundedQueue { public static void main(String[] args) { QueueInterface test = new ArrayBoundedQueue(); Scanner scan = new Scanner(System.in);

String skip; // skip end of line after reading an integer boolean keepGoing; // flag for "choose operation" loop int constructor; // indicates user's choice of constructor int operation; // indicates user's choice of operation

String enqueueString = "", dequeueString = ""; // used by operations

// Handle test name System.out.println("What is the name of this test?"); String testName = scan.nextLine(); System.out.println(" This is test " + testName + " "); // Handle constructor System.out.println("Choose a constructor:"); System.out.println("1: ArrayBoundedQueue( )"); System.out.println("2: ArrayBoundedQueue(int maxSize)"); if (scan.hasNextInt()) constructor = scan.nextInt(); else { System.out.println("Error: you must enter an integer."); System.out.println("Terminating test."); return; } skip = scan.nextLine();

switch (constructor) { case 1: test = new ArrayBoundedQueue(); break; case 2: System.out.println("Enter a maximum size:"); int maxSize; if (scan.hasNextInt()) maxSize = scan.nextInt(); else { System.out.println("Error: you must enter an integer."); System.out.println("Terminating test."); return; } skip = scan.nextLine(); test = new ArrayBoundedQueue(maxSize); break; default: System.out.println("Error in constructor choice. Terminating test."); return; } // Handle test cases keepGoing = true; while (keepGoing) { System.out.println(" Choose an operation:"); System.out.println("1: enqueue(element)"); System.out.println("2: String dequeue()"); System.out.println("3: boolean isFull()"); System.out.println("4: boolean isEmpty()"); System.out.println("5: int size()"); System.out.println("6: stop Testing"); if (scan.hasNextInt()) operation = scan.nextInt(); else { System.out.println("Error: you must enter an integer."); System.out.println("Terminating test."); return; } skip = scan.nextLine();

switch (operation) { case 1: // enqueue System.out.println("Enter string to enqueue:"); enqueueString = scan.nextLine(); System.out.println("enqueue(\"" + enqueueString + "\")"); try { test.enqueue(enqueueString); } catch (QueueOverflowException QOFException) { System.out.println("Overflow Exception: " + QOFException.getMessage()); } break; case 2: // dequeue System.out.println("dequeue()"); try { dequeueString = test.dequeue(); } catch (QueueUnderflowException QUFException) { System.out.println("Underflow Exception: " + QUFException.getMessage()); break; } System.out.println("Result: " + dequeueString + " was returned."); break; case 3: // isFull System.out.println("isFull()"); System.out.println("Result: " + test.isFull()); break; case 4: // isEmpty System.out.println("isEmpty()"); System.out.println("Result: " + test.isEmpty()); break; case 5: // size System.out.println("size()"); System.out.println("Result: " + test.size()); break; case 6: // stop testing keepGoing = false; break; default: System.out.println("Error in operation choice. Terminating test."); return; } }

System.out.println("End of Interactive Test Driver"); } }

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!