In Java please. Create a HeapPriorityQueue interface with the following abstract
ID: 3713478 • Letter: I
Question
In Java please.
Create a HeapPriorityQueue interface with the following abstract methods. isEmpty, isFull, enqueue, dequeue, reheapifyUpward, reheapifyDownward, reposition b) Create the HeapPriorityQueue class. Have Heap Array hold maxSize of 250 entries. Also, include the methods: default constructor, toString. c) Create HeapOverflowException and HeapUnderflowException classes d) Create a HeapDemo class that creates a HeapPriorityQueue object and insert the values 1 -10 into the heap Print out the heap and remove two values from the heap Print the resulting heap Try and show the resulting tree with the nodes on their appropriate levels along with their branches.
Explanation / Answer
Answer
a) The Java Program
//Include libraries
import java.util.Scanner;
//Define class Task
class Task
{
//Declare variables
String job;
int priority;
//Define constructor
public Task (String job, int priority)
{
//Set job value
this. job = job;
//Set priority value
this. priority = priority;
}
//Define toString ( )
public string toString ( )
{
//Return values
return "Job Name : "+ job +" priority : "+priority;
}
}
//Define class HeapPriorityQueue
class HeapPriorityQueue
{
//Define heap
private Task [ ] heap;
//Declare variables
private int heapSize, capacity;
//Define Constructor
public HeapPriorityQueue(int capacity)
{
//set values
this.capacity = capacity + 1;
//set heap
heap = new Task [this.capacity];
//set values
heapsize = 0;
}
//Define clear ( )
public void clear ( )
{
//set hep
heap = new Task [capacity];
//set value as 0
heapsize = 0;
}
//Define isEmpty ( )
public boolean isEmpty ( )
{
//Return value
return heapsize ==0;
}
//Define isFull ( )
public boolean isFull ( )
{
//check if full
return heapSize == capacity - 1;
}
//Define size ( )
public int size ( )
{
//Return size
return heapsize;
}
//Define insert ( )
public void insert (string job, int priority)
{
//Define newJob
Task newJob = new Task (job, priority);
//Set value
heap [++heapsize] = newJob;
//Set pos value
int pos = heapSize;
//Loop until value not 1
while (pos ! = 1 && newJob.priority > heap [pos/2].priority)
{
//set array values
heap [pos] = heap[pos/2];
//Set new value
pos /=2;
}
//Assign to array
heap [pos] = newJob;
}
//Define remove ( )
public Task remove ( )
{
//Declare variables
int parent, child;
Task item, temp;
//If it is empty
if (isEmpty( ) )
//Display message
system.out.println("Heap empty");
//Return Null
return null;
}
//Set first value
item = heap [1];
//Set temp
temp = heap[heapSize--];
//Set values
parent = 1;
child = 2;
//Loop until condition
while (child <= heapsize)
{
//If child less than heapsize
if (child < heapsize && heap [child].priority <
heap [child + 1] . priority)
//Increment child
child++;
//if priority is greater
if (temp.priority >= heap [child].priority)
//Break
break;
//Assign values
heap [parent] = heap [child];
//Assign value
parent = child;
//Set new value
child *= 2;
}
//Assign temp to array
heap[parent] = temp;
//Return item
return item;
}
}
//Define class HeapDemo
public class HeapDemo
{
//Define main method
public static void main(String [ ] args)
{
//Define scanner variable
Scanner scan = new scanner (system.in);
//Display message
System.out.println("Enter size of priority queue");
//Define pq
HeappriorityQueue pq = new
HeapPriorityQueue(scan.nextInt( ) );
//Define ch
char ch;
//Define loop
do
{
//Display menu
System.out.println (" Priority Queue operation ");
System.out.println ("1. insert");
System.out.println ("2. remove");
System.out.println ("3. check empty");
System.out.println ("4. check full");
System.out.println ("5. clear");
System.out.println ("6. size");
//Store choice
int choice = scan.nextInt ( ) ;
//Define switch statement
switch (choice)
{
//Define case 1
case 1 :
//Display message
System.out.println ("Enter job name and priority") ;
//lterating over Priority Queue, iterator returned
//by PriorityQueue doesn't guarantee any order
Iterator<Integer> itr = pq.iterator();
System.out.println("iterating over PriorityQueue");
while(itr.hsNext()){
System.out.println(itr.next());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.