For Java, implement a priority queue. The methods that have to be supported are:
ID: 3830327 • Letter: F
Question
For Java, implement a priority queue.
The methods that have to be supported are:
(1) Test if the heap is empty.
(2) Count the number of active processes.
(3) Print the set of active processes, with or without their priorities.
(4) Insert a new process with a specified priority
(5) Observe the maximum priority process, printing the process ID and its priority.
(6) Delete the maximum priority process, returning the process ID.
(7) Increase the priority of a given process by a specified delta.
(8) Decrease the priority of a given process by a specified delta.
(9) Change the priority of a given process to a given value.
(10) Delete an arbitrary process.
Explanation / Answer
/** ** Java Program to implement Priority Queue **/ import java.util.Scanner; /** class Task **/ class Task { String job; int priority; /** Constructor **/ public Task(String job, int priority) { this.job = job; this.priority = priority; } /** toString() **/ public String toString() { return "Job Name : "+ job +" Priority : "+ priority; } } /** Class PriorityQueue **/ class PriorityQueue { private Task[] heap; private int heapSize, capacity; /** Constructor **/ public PriorityQueue(int capacity) { this.capacity = capacity + 1; heap = new Task[this.capacity]; heapSize = 0; } /** function to clear **/ public void clear() { heap = new Task[capacity]; heapSize = 0; } /** function to check if empty **/ public boolean isEmpty() { return heapSize == 0; } /** function to check if full **/ public boolean isFull() { return heapSize == capacity - 1; } /** function to get Size **/ public int size() { return heapSize; } /** function to insert task **/ public void insert(String job, int priority) { Task newJob = new Task(job, priority); heap[++heapSize] = newJob; int pos = heapSize; while (pos != 1 && newJob.priority > heap[pos/2].priority) { heap[pos] = heap[pos/2]; pos /=2; } heap[pos] = newJob; } /** function to remove task **/ public Task remove() { int parent, child; Task item, temp; if (isEmpty() ) { System.out.println("Heap is empty"); return null; } item = heap[1]; temp = heap[heapSize--]; parent = 1; child = 2; while (childRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.