GTU OOP Program - 24

24). Define MYPriorityQueue class that extends Priority Queue to implement the Cloneable interface and implement the clone() method to clone a priority queue.

Class Program_24(Program_24.java)

public class Program_24 {
    public static void main(String[] args) throws CloneNotSupportedException {
        MyPriorityQueue<Integer> q1 = new MyPriorityQueue<>();
        q1.offer(10);
        q1.offer(20);
        q1.offer(50);
        MyPriorityQueue<Integer> q2 = q1.clone();
        System.out.print("Queue 1: ");
        while (q1.size() > 0) {
            System.out.print(q1.remove() + " ");
        }
        System.out.println();

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

Class MyPriorityQueue (MyPriorityQueue.java)

import java.util.PriorityQueue;

public class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable {
    @Override
    public MyPriorityQueue<E> clone() throws CloneNotSupportedException {
        MyPriorityQueue<E> temp = new MyPriorityQueue<>();
        temp.addAll((MyPriorityQueue<E>) super.clone());
        return temp;
    }
}

Output
cse gtu, gtu study material for computer engineering students gujarat
technological university oop lab manual solution

gtu oop lab manual,

oop GTU Practical,

oop Programs,

Object oriented Programming,

Gtu study material 3140705,

OOP 3140705,

oop practical list,

Java Programming,

gtu oop practical,

oop lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Programs - 11

Mini project ideas for Operating System

GTU OS Program - 9