this is java problem. You will write Comparators to compare Job objects based on
ID: 3708752 • Letter: T
Question
this is java problem.
You will write Comparators to compare Job objects based on priority and security, as well as write test code that illustrates sorting using the Comparators and code that utilizes a PriorityQueue.
Create a package named prob2.
Consider the Job class below. Notice that it has integer fields for priority and security. Create a Job class and copy the code below. In this scenario, a larger priority means that it is more urgent than one with a lower priority. Also, a larger security means means that there is more possible danger/risk than one with a lower security.
public class Job {
private int securityLevel;
private int priority;
public Job(int priority, int securityLevel) {
this.priority = priority;
this.securityLevel = securityLevel;
}
public int getSecurityLevel() {
return securityLevel;
}
public int getPriority() {
return priority;
}
@Override
public String toString() {
return "(p-" + priority + ",s-" + securityLevel + ")";
}
}
Write a Comparator named PriorityComparator that compares Job objects based on their priority and if priority is the same, then based on security. The order should be high priority followed by high security.
Write a Comparator named SecurityComparator that compares Job objects based on their security and if security is the same, then based on priority. The order should be high security followed by high priority.
Write a JobManager class with these members:
Description A PriorityQueue of Job obiects True if the PriorityQueue is ordered on the PriorityComparotorand false f ordered on the SecurityComparator Constructor, initializes the priority queue with the proper comparator Offers a job to the priority queue Returns the Job at the head of the priority queue (without removing it Removes and returns the Job at the head of the priority queue Changes isPriority and changes the priprity queue to the value specified In other words, if isPriority-true in the JobManager object and setPrioritufalse) is called, then jobs is converted to a priority queue using the SecurityComparator Returns the value of isPriorit Returns true if iobs is empty and false otherwise Empties iobs and returns all the iobs in an arrav list Member obs isPriority obManager(isPriority:boolearn offer(iob:Job eak():Job oll):Job setPriority(isPriority:boolean etPrioritv():boolean sEm flushl:ArrayList :boolearn c. Create a class named JobManagerTester with a main with code to test your JobManagerExplanation / Answer
package prob2; import java.util.Comparator; public class PriorityComparator implements Comparator { @Override public int compare(Job job1, Job job2) { int cmp = Integer.compare(job2.getPriority(), job1.getPriority()); if(cmp == 0) { return Integer.compare(job2.getSecurityLevel(), job1.getSecurityLevel()); } else { return cmp; } } } package prob2; import java.util.Comparator; public class SecurityComparator implements Comparator { @Override public int compare(Job job1, Job job2) { int cmp = Integer.compare(job2.getSecurityLevel(), job1.getSecurityLevel()); if(cmp == 0) { return Integer.compare(job2.getPriority(), job1.getPriority()); } else { return cmp; } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.