Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC 205 Lab 11: Queues Goals After completing this lab, you should be able to: Understand and be able to use the methods of the Queue ADT. Know some of the general applications of queues. Be able to write simple class methods to count the number of elements in a queue and print out a queue. Understand how to implement a queue using a circular array. Be able to simulate queues using the LinkedList class from the Java Collections Framework. Lab Startup The source code files we are going to use as a code base are located in the following directory. Courses on ‘Blackhawk’\Martin Zhao\205\labs\lab11 Make a folder for you on the C: drive temporarily or on a floppy disk. Make a directory (desirably csc205/labs/lab11) on Cobra also and ftp your code there after you’ve finished. Turn in your answer sheet or sent your answers by email. Building and Tracing a Queue Draw the stack that would be created following the code segment below. Use your Queue ADT handout as a guide. char x, y; Queue q = new Queue(); q.enqueue(new Character('W')); x = ((Character) q.front()).charValue(); q.enqueue(new Character('V')); q.enqueue(new Character(x)); q.dequeue(); y = ((Character) q.front()).charValue(); q.dequeue(); q.enqueue(new Character('T')); q.dequeue(); q.enqueue(new Character(x)); q.enqueue(new Character(y)); x = ((Character) q.front()).charValue(); q.enqueue(new Character(x)); Now, compile and run the MyQueue program to check your results. Writing Simple Class and Instance Methods with Queues Add a value-returning class method named letterCount to your QCount program that can be used to return the total number of letters in a queue. For example, if the queue were: b $ E + I y s # the call letterCount(q)would return the integer 5. Your method will have one parameter, a queue object, and should not modify this parameter. You are writing this method assuming you are a user of the Queue class. Here, you’ll merely be using the methods of the Queue class to achieve your solution, and do not worry or carry about the fact that it is really built using a circular array. Add a call to your QCount program to test your method when you’re done. [Note: see comments on page 4.] Now, let’s switch gears and add a letterCount method to the implementation file of our Queue class. So, within the file Queue.java, add a value-returning instance method named letterCount that returns an integer representing the total number of letters stored in your circular array. Here, we do care about the fact that our queue is built as a circular array. The easiest algorithm here is to set up an index that starts at your front postion, and advances through your array n times where n is the size of your queue. Remember to reset front properly so that it circles back to the beginning. Test your method by invoking a call to letterCount from the q object in your main method. Using the LinkedList class from the Java Collections Framework to Simulate Queues In the Java collections framework, you of course will find a built-in LinkedList class. You will not find a built-in Queue class however. The reason why is that you already have everything you need to simulate a queue using methods from the LinkedList class from the Java Collections Framework. Visit Sun’s website to have a look at the Java API (http://java.sun.com/j2se/1.4/docs/api/). Look up the upper-left panel for the java.util package, and then select LinkedList class in the lower-left panel. There are a plethora of wonderful methods available to you in the LinkedList class! Notice that you have methods like addLast() for enqueueing, removeFirst() for dequeueing, get(int index) for retrieving an item at a particular index, etc. You 2 could also easily simulate a deque data structure (pronounced “deck”). This is a data structure of values that allows both insertions and deletions from the front and back. Think of a “deck of cards” or a bus with both entrances and exits at the front and back. Now, take a look at the JCFQueue program that has already been set up for you. Two “queue” objects have already been declared for you. Notice they are both of the type LinkedList. The Check for Identical Queues You will want to complete the body of the method identicalCheck that takes two simulated queue objects of type LinkedList, and returns true or false indicating whether these two objects are the same. Two queues would be identical if they are the same size and they each contain the same items in the same position. So, using the methods of the LinkedList class perform this check. The Search and Replace Method Now, uncomment out the body of the call to the findAndReplace method and the call to it within main. We want to complete the body of this method, which takes a queue object simulated using a LinkedList, and replaces all occurrences of the key object which is sent in as a parameter with a newVal object which is sent in as a parameter. For example, if let’s say we send in our queue1 object which is shown below and a key value of ‘$’ and a replace value of ‘*’. b $ E $ Our queue will be returned as : b * E * The Print Method Finally, let’s add a method that can be used to print our queue object that is simulated as LinkedList so we can be sure our previous method works correctly. If the queue is empty, print a message indicating so. Do not modify your queue in any way. A call to test this method has already been set up for you within the main method. 3 [Note] Notice that the headings of both methods (main and letterCount) in QCount have a clause throws CloneNotSupportedException. This is because you are calling the clone method, which is inherited from the Object class, but the Queue class didn’t implement the Cloneable interface. The exception will be thrown when clone is invoked, and since it’s not handled within the method, the throws clause must be used in its method declaration and in that of main. Both throws clause can be removed if one of the following two things is done: 1. add Cloneable to the implements clause in the Queue class heading, or 2. use a try block to catch CloneNotSupportedException within QCount. The first solution is better if we meant to clone our Queue objects. Try either or both of them. 4