Question
Problem 1: Maintain a task heap You have a set of tasks and each task has its priority. A task is a pair [String name,
Problem 1: Maintain a task heap
You have a set of tasks and each task has its priority. A task is a pair [String name, Integer priority]. For example, [Task1, 5], [Task2, 2]. We need to add these tasks into a priority queue. As we know, heaps are usually used as the underlying data structures of priority queues. In Java, there are several constructors for implementing a priority queue. Please see the reference below:
https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
Part a. Regarding the default priority queue constructor, can you show it (PriorityQueue) is implemented with Min-Heap, that is the top element is the minimum one in the heap. Write a program to demonstrate. Attach your code and screenshots of the output. You need to add a set of tasks (with priority) into the priority queue first. For example, you add [Task1, 5], [Task2, 2], [Task3, 1], and you should show (print out) the following in your output after utilizing the poll method (check the reference link above for details).
[Task3, 1]
[Task2, 2]
[Task1, 5]
Part b. Can you construct a Max-Heap using a comparator? Please review the sample code in Lecture 4 Activity 1. Write a program to demonstrate. Attach your code and screenshots of the output. For example, you add [Task1, 5], [Task2, 2], [Task3, 1], and you should show (print out) the following in your output after utilizing the poll method (check the reference link above for details).
[Task1, 5]
[Task2, 2]
[Task3, 1]
Problem 2: Scheduling jobs on a CPU
One of the main applications of priority queues is in operating systemsfor scheduling jobs on a CPU. In this project you are to build a program that schedules simulated CPU jobs. Your program should run in a loop, each iteration of which corresponds to a time slice for the CPU. Each job is assigned a priority, which is an integer between20 (highest priority) and 19 (lowest priority), inclusive. From among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority. In this simulation, each job will also come with a length value, which is an integer between 1 and 100, inclusive, indicating the number of time slices that are needed to process this job. For simplicity, you may assume jobs cannot be interruptedonce it is scheduled on the CPU, a job runs for a number of time slices equal to its length. Your simulator must output the name of the job running on the CPU in each time slice and must process a sequence of commands, one per time slice, each of which is of the form add job name with length n and priority p or no new job this slice.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started