Question: Exercise 1 Download CircularArrayQueue.java, QueueADT.java and EmptyCollectionException.java from the sample code page and fill in the missing code in the methods. The code for the

Exercise 1

Download CircularArrayQueue.java, QueueADT.java and EmptyCollectionException.java from the sample code page and fill in the missing code in the methods. The code for the helper method expandCapacity is given; note that it is different from the expandCapacity of the regular array implementations that we have seen for the Stack ADT and Queue ADT.

first is similar to dequeue, but doesn't remove the element

isEmpty and size are trivial

for toString, setting up the string is similar in concept to the toString method in ArrayStack.java, which was similar to the following:

public String toString(){ String result = ""; for (int i=0; i < top; i++) result = result + stack[i].toString() + " "; return result; } 

Of course the attributes are no longer called stack and top, so you need to change those. But, more importantly, you will not be starting the array index at 0. The counter for the loop can still go from 0 to count, since you will still go through the for loop as many times as there are items in the queue. But the array index will need to be a different variable, that starts at the value in the attribute front. You will need to increment the array index in the body of the for loop, taking into account that it may need to wrap around to the beginning of the array again. It is the same idea as how rear is incremented in the enqueue method, and how front is incremented in the provided expandCapacity method.

Exercise 2

Download the given test driver TestCAQ.java to test your implementation of the CircularArrayQueue class. This test driver is provided as an example of a test program that does thorough testing, in a way that displays whether or not the test results are the expected results.

Answer the questions below before running TestCAQ on your circular array queue implementation, to make sure you understand how it works. Make corrections in your CircularArrayQueue.java until all the tests have been completed successfully.In the main method, what is the purpose of the second parameter in all the calls to the testing methods, for example the first 3 calls:

t_isEmpty(first, true); t_size(first, 0); t_toString(first, ""); 

Is it possible to enqueue objects of different types to the queue first, for example:

first.enqueue(new Integer()); first.enqueue(new Float(i*10)); 

What is the type of the objects being enqueued in the following statements?

first.enqueue("A"); first.enqueue("S");

CircularArrayQueue.java

/** * CircularArrayQueue represents an array implementation of a queue in * which the indexes for the front and rear of the queue circle back to 0 * when they reach the end of the array. * * @author Dr. Lewis * @author Dr. Chase * @version 1.0 08/12/08 */ public class CircularArrayQueue implements QueueADT { private final int DEFAULT_CAPACITY = 100; private int front, rear, count; private T[] queue; /** * Creates an empty queue using the default capacity. */ public CircularArrayQueue() { front = rear = count = 0; queue = (T[]) (new Object[DEFAULT_CAPACITY]); } /** * Creates an empty queue using the specified capacity. * * @param initialCapacity the integer representation of the initial * size of the circular array queue */ public CircularArrayQueue (int initialCapacity) { front = rear = count = 0; queue = ( (T[])(new Object[initialCapacity]) ); } /** * Adds the specified element to the rear of this queue, expanding * the capacity of the queue array if necessary. * * @param element the element to add to the rear of the queue */ public void enqueue (T element) { if (size() == queue.length) expandCapacity(); queue[rear] = element; rear = (rear+1) % queue.length; count++; } /** * Removes the element at the front of this queue and returns a * reference to it. Throws an EmptyCollectionException if the * queue is empty. * * @return the reference to the element at the front * of the queue that was removed * @throws EmptyCollectionException if an empty collections exception occurs */ public T dequeue() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException ("queue"); T result = queue[front]; queue[front] = null; front = (front+1) % queue.length; count--; return result; } /** * Returns a reference to the element at the front of this queue. * The element is not removed from the queue. Throws an * EmptyCollectionException if the queue is empty. * * @return a reference to the first element in the * queue * @throws EmptyCollectionException if an empty collections exception occurs */ public T first() throws EmptyCollectionException { // left as programming project } /** * Returns true if this queue is empty and false otherwise. * * @return returns true if this queue is empty and false if otherwise */ public boolean isEmpty() { // left as programming project } /** * Returns the number of elements currently in this queue. * * @return the integer representation of the size of this queue */ public int size() { // left as programming project } /** * Returns a string representation of this queue. * * @return the string representation of this queue */ public String toString() { // left as programming project } /** * Creates a new array to store the contents of this queue with * twice the capacity of the old one. */ public void expandCapacity() { T[] larger = (T[])(new Object[queue.length *2]); for(int scan=0; scan < count; scan++) { larger[scan] = queue[front]; front=(front+1) % queue.length; } front = 0; rear = count; queue = larger; } }
EmptyCollectionException.java /** * @author Lewis and Chase * * Represents the situation in which a collection is empty. */ public class EmptyCollectionException extends RuntimeException { /** * Sets up this exception with an appropriate message. * @param collection String representing the name of the collection */ public EmptyCollectionException (String collection) { super ("The " + collection + " is empty."); } }

TestCAQ.java

/* ** A java class that tests the implementation of CircularArrayQueue ** ** @author Ben Stephenson ** @version 0.2 ** edited for CS1027 2007, 2009 ** */ public class TestCAQ { // test isEmpty method public static void t_isEmpty(CircularArrayQueue it, boolean expected) { System.out.println("Testing isEmpty: "); if (it.isEmpty() == expected) { System.out.println("Passed "); } else { System.out.println("Failed "); } } // test size method public static void t_size(CircularArrayQueue it, int expected) { System.out.println("Testing size: "); if (it.size() == expected) { System.out.println("Passed "); } else { System.out.println("Failed"); System.out.println(" Expected: '" + expected + "'"); System.out.println(" Got: '" + it.size() + "'"); System.out.println(""); } } // test toString method public static void t_toString(CircularArrayQueue it, String expected) { System.out.println("Testing toString: "); if (it.toString().equals(expected)) { System.out.println("Passed "); } else { System.out.println("Failed"); System.out.println(" Expected: '" + expected + "'"); System.out.println(" Got: '" + it.toString() + "'"); System.out.println(""); } } // test dequeue method public static void t_dequeue(CircularArrayQueue it, String expected) throws Exception { Object obj; System.out.println("Testing dequeue: "); obj = it.dequeue(); if (obj.toString().equals(expected)) { System.out.println("Passed "); } else { System.out.println("Failed"); System.out.println(" Expected: '" + expected + "'"); System.out.println(" Got: '" + obj.toString() + "'"); System.out.println(""); } } public static void main(String args[]) throws Exception { CircularArrayQueue first; // first queue used in testing int i; // for loop counter first = new CircularArrayQueue(); // create empty queue // // Empty queue tests // t_isEmpty(first,true); t_size(first,0); t_toString(first,""); // // Add some items of differing types -- every third item is an Integer // instead of being a Float // for (i = 0; i < 15; i++) { if (i % 3 == 0) { first.enqueue(new Integer(i)); } else { first.enqueue(new Float(i * 10)); } } // // Queue tests now that it has some elements // t_isEmpty(first,false); t_size(first,15); t_toString(first,"0 10.0 20.0 3 40.0 50.0 6 70.0 80.0 9 100.0 110.0 12 130.0 140.0 "); // // Remove 2 elements and make sure they are correct // t_dequeue(first,"0"); t_dequeue(first,"10.0"); // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,false); t_size(first,13); t_toString(first,"20.0 3 40.0 50.0 6 70.0 80.0 9 100.0 110.0 12 130.0 140.0 "); first.enqueue("A"); first.enqueue("S"); first.enqueue("D"); first.enqueue("F"); // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,false); t_size(first,17); t_toString(first,"20.0 3 40.0 50.0 6 70.0 80.0 9 100.0 110.0 12 130.0 140.0 A S D F "); // // Remove 10 items from the queue // for (i = 0; i < 10; i++) { first.dequeue(); } // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,false); t_size(first,7); t_toString(first,"12 130.0 140.0 A S D F "); // // Add 3 more integers // for (i = 0; i < 3; i++) { first.enqueue(new Integer(-i)); } // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,false); t_size(first,10); t_toString(first,"12 130.0 140.0 A S D F 0 -1 -2 "); // // Remove 10 items from the queue // for (i = 0; i < 10; i++) { first.dequeue(); } // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,true); t_size(first,0); t_toString(first,""); // // Try to dequeue another item when the queue is empty // System.out.println("Testing dequeue from an empty queue:"); try { first.dequeue(); } catch (NullPointerException e) { System.out.println("Failed with a Null Pointer Exception "); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Failed with an Array Index Out Of Bounds Exception "); } catch (Exception e) { System.out.println("Caught an exception: '" + e + "'"); System.out.println("Assuming that it passed but if this is a system exception instead"); System.out.println("of an exception that you intended to throw then it failed."); System.out.println(""); } // // Adding 1 item to the queue // first.enqueue("Hello World"); // // Verify that isEmpty, size and toString still give correct results // t_isEmpty(first,false); t_size(first,1); t_toString(first,"Hello World "); // // Test removal with only 1 element // t_dequeue(first,"Hello World"); // // Add lots of elements // for (i = 0; i < 1000; i++) { first.enqueue(new Float(i)); } // // Verify that isEmpty and size give correct results // t_isEmpty(first,false); t_size(first,1000); } }                                            
                    

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!