Q:

(Clone PriorityQueue) Define MyPriorityQueue class that extends PriorityQueue to implement the Cloneable interface and implement the clone() method to clone a priority queue

0

(Clone PriorityQueue) Define MyPriorityQueue class that extends PriorityQueue to implement the Cloneable interface and implement the clone() method to clone a priority queue.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/**********************************************************************************
* (Clone PriorityQueue) Define MyPriorityQueue class that extends PriorityQueue   *
* to implement the Cloneable interface and implement the clone() method to clone  *
* a priority queue.                                                               *
**********************************************************************************/
public class Exercise_20_12 {
	public static void main(String[] args) throws CloneNotSupportedException {
		// Create a string MyPriorityQueue and 4 state names
		MyPriorityQueue<String> queue1 = new MyPriorityQueue<>();
		queue1.offer("Oklahoma");
		queue1.offer("Indiana");
		queue1.offer("Georgia");
		queue1.offer("Texas");

		// Clone queue1 to queue2
		MyPriorityQueue<String> queue2 = queue1.clone();

		// Add 2 more state names to queue 1
		queue1.offer("New York");
		queue1.offer("New Jersey");

		// Remove 1 state from queue 2
		queue2.remove();
		
		// Display queues
		System.out.print("Queue1: ");
		while (queue1.size() > 0) {
			System.out.print(queue1.remove() + " ");
		}
		System.out.println();

		System.out.print("Queue2: ");
		while (queue2.size() > 0) {
			System.out.print(queue2.remove() + " ");
		}
		System.out.println();
	}
}

MyPriorityQueue.java 

import java.util.*;

public class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable {
	@Override /** Override the protected clone 
		method defined in the Object class */
	public MyPriorityQueue<E> clone() throws CloneNotSupportedException {
		MyPriorityQueue<E> temp = new MyPriorityQueue<>();
		temp.addAll((MyPriorityQueue<E>)super.clone());
		return temp;
	}

}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now