Data Structure and Algorithm (Priority Queues) Please take your time figuring it
ID: 3812654 • Letter: D
Question
Data Structure and Algorithm (Priority Queues) Please take your time figuring it out because I want an editable source code that can be run in IDE. Good luck and thank you. 4. Priority queue You have been asked to test the effectiveness of the following different scheduling algorithms for different types of queuing applications. 1) First-In-First-out (FIFo: The jobs are processed in the arriving order. The job at the front of the queue is served until it has completed. That job is then removed from the queue. The next job at the front of the queue is served until it has completed and then it is removed from the queue. This process is continued until the queue is empty. 2) Shortest-Job-First (SJF): The job with the shortest processing time is processed first until it has completed. That job is then removed from the queue. The next job with smallest processing is then served until it has completed and then it is removed fro the queue. This process is continued until the queue is empty.Explanation / Answer
import java.util.*; /** * This class Schedule the joba as per Scheduler algorithm */ public class JobScheduler { Scanner scanner = new Scanner(System.in); private class Job { String jobName; long enqueTimeStamp; long executionTime; public Job(String jobName, long enqueTimeStamp, long executionTime) { this.jobName = jobName; this.enqueTimeStamp = enqueTimeStamp; this.executionTime = executionTime; } } public static void main() { JobScheduler jobScheduler = new JobScheduler(); List priorityQueue = null; while(true) { System.out.println(); System.out.println("Job Scheduler"); System.out.println("1. Create new Jobs Queue"); System.out.println("2. Execute using FIFO"); System.out.println("3. Execute using SJF"); System.out.println("0. Quite"); System.out.println("Enter you choice :"); int choice = jobScheduler.scanner.nextInt(); switch(choice) { case 1: priorityQueue = jobScheduler.createJobsQueue(); break; case 2: if(priorityQueue == null){ System.out.println("Create queue first"); break; } jobScheduler.executeUsingFIFO(priorityQueue); break; case 3: if(priorityQueue == null){ System.out.println("Create queue first"); break; } jobScheduler.executeUsingSJF(priorityQueue); break; case 0: jobScheduler.scanner.close(); System.exit(0); break; default: System.out.println("Invalide choice"); } System.out.println(); } } private void executeUsingSJF(List priorityQueue) { Collections.sort(priorityQueue, new Comparator() { @Override public int compare(Job o1, Job o2) { return (int)(o1.executionTime - o2.executionTime); } }); for(Job job: priorityQueue) { System.out.println("JobName: " + job.jobName); System.out.println("JobEnqueTime: " + job.enqueTimeStamp); System.out.println("JobExecutionTime: " + job.executionTime); } } private void executeUsingFIFO(List priorityQueue) { Collections.sort(priorityQueue, new Comparator() { @Override public int compare(Job o1, Job o2) { return (int)(o1.enqueTimeStamp - o2.enqueTimeStamp); } }); for(Job job: priorityQueue) { System.out.println("JobName: " + job.jobName); System.out.println("JobEnqueTime: " + job.enqueTimeStamp); System.out.println("JobExecutionTime: " + job.executionTime); } } private List createJobsQueue() { List queue = new ArrayList(); System.out.println("How many jobs you want to create :"); int numberOfJobs = scanner.nextInt(); for(int i=1;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.