CSIS 3103 - Lab 4 : Copying Queues
Oct 27, 2010
You can work in pairs for this lab.
Objectives:
Open Eclipse and create a new project.
Add a class called QueueCopy to the project. Replace the auto-generated code with the following:
public class QueueCopy { /** post: q is the same as it was before the method was called * @param q The queue to copy * @return A copy of q */ public static <E> Queue<E> copy(Queue<E> q) { // TODO: Complete this method // Only use add, offer, element, peek, poll, remove, and size methods from the Queue interface return null; // replace null with the actual return value }
/** post: q is the same as it was before the method was called * @param q The queue to copy * @return A copy of q with elements in reverse order */ public static <E> Queue<E> reverseCopy(Queue<E> q) { // TODO: Complete this method // Only use add, offer, element, peek, poll, remove, and size methods from the Queue interface // The Deque class may also be used return null; // replace null with the actual return value }
public static void main(String[] args) { // Create a Queue and put at least 5 elements in it // Use the copy method to make a duplicate queue
System.out.println("Original queue"); // Use a for-each style loop to print the original queue
System.out.println("Copy queue"); // Use a for-each style loop to print the copy // Use the reverseCopy method to make a copy of the original queue in reverse order System.out.println("Original queue"); // Use a for-each style loop to print the original queue
System.out.println("Reverse copy queue"); // Use a for-each style loop to print the reverse copy } }
1. Implement the copy method
Implement the copy method in the QueueCopy class. The returned queue should be an exact duplicate of the parameter q. When the method returns, q should be the same as it was before the method was called. You can only use the add, offer, element, peek, poll, remove, and size methods from the Queue interface to do this.
2. Update the main method
In the main method, create a queue and put at least 5 elements in it. Then make a duplicate queue by calling the copy method. Then write loops to display the contents of the two queues to verify that they are the same.
3. Implement the reverseCopy method
Implement the reverseCopy method in the QueueCopy class. The returned queue should have the elements of the parameter q but in reverse order. When the method returns, q should be the same as it was before the method was called. You can only use the add, offer, element, peek, poll, remove, and size methods from the Queue interface and methods from the Deque class to do this.
4. Finish the main method
In the main method, make a reversed queue by calling the reverseCopy method and write loops to display the contents of the two queues to verify the results.
Hand In:
Submit the completed QueueCopy.java class.