Figure 1: A priority queue based on the unit price of medicines, with the given
ID: 3818863 • Letter: F
Question
Figure 1: A priority queue based on the unit price of medicines, with the given treatments.csv file as the source of data. Lower price is considered better. 2 Tasks The details of these tasks, in terms of programming implementations, are described in this section. First, look at HealthCenter.java. It contains the main method to run this application, and demonstrates how to impose a total order on treatments, based on the price of the medicines. A sample output is shown in Fig. 1. The Treatment Class: The basic structure of a treatment (i.e., medication) is provided in this class. You must write the toString, and equals methods for this class, as well as any constructor(s). Additionally, the comparators used in the main method also appear in this class. That is, you will have to implement the two comparators • PriceBasedTreatmentComparator, and • SuccessBasedTreatmentComparator. The Heap Interface and the Priority Queue Class: The heap interface is already defined, with its methods explained in the Javadoc. Follow the documentation provided in this interface to complete the priority queue class. 2.1 Notes a) Do NOT change any code already provided, including the main method. b) The printed output that you obtain must be in the exact same format as shown in Fig. 1. c) There may be methods that you will need to write, but not mentioned in the given code. d) It is a part of this assignment for you to figure out how to write the code so that the main method as it has been provided works without any change.
Run Health Center /java /Library/Java/JavavirtualMachines/jdkl.8.0_121.jdk/Contents/Home/bin Treatment name "drugA diseaseTreated influenzaA probabilityofSuccess 0.74, pricePerunita1.5) Treatment name "drugD diseaseTreated 'influenzaA probabilityofsuccess 0.65, pricePerUnit 1.7) Treatment name "drugC 'influenzaA 0.6, pricePerunit 1.5) ll Eo Treatment name druge', diseaseTreated influenzaA probabilityofSuccess 0.88, pricePerunit 1.85 Treatment iname "drugE diseaseTreated-'infuuenzaA', probabilityofSuccess 0.75, pricePerunit 2.25) Treatment name "drugF diseaseTreated 'influenzaA probability0fSuccess 0.88, pricePerunit 3.25) Treatment name "drug diseaseTreated "influenzaA probabilityofSuccess 0.9, pricePerunita4.e) Treatment iname drugH', diseaseTreated infuuenzaA probability of success 0.94, pricePerunit 6.751Explanation / Answer
Here is the Treatment class(containing price based and success based comparator) and PriorityQueue class which has creates the priorityQueue using those comparator.
Implemented the following:
PriceBasedTreatmentComparator, SuccessBasedTreatmentComparator and fromCollection method in PriorityQueue.
Code:
class Treatment {
String name;
String diseaseTreated;
double probabilityOfSuccess;
double pricePerUnit;
Treatment(){
name="";
}
Treatment(String name,String diseaseTreated,double probabilityOfSuccess, double pricePerUnit){
this.name=name;
this.diseaseTreated=diseaseTreated;
this.probabilityOfSuccess=probabilityOfSuccess;
this.pricePerUnit=pricePerUnit;
}
@Override
public int hashCode() {
int result;
long temp;
result = name.hashCode();
result = 31 * result + diseaseTreated.hashCode();
temp = Double.doubleToLongBits(probabilityOfSuccess);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(pricePerUnit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public Comparator PriceBasedTreatmentComparator() {
// TODO Auto-generated method stub
class PriceBasedComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Treatment t1=(Treatment)o1;
Treatment t2=(Treatment)o2;
if(t1.pricePerUnit<t2.pricePerUnit)
return -1;
else if(t1.pricePerUnit>t2.pricePerUnit)
return 1;
return 0;
}
}
return new PriceBasedComparator();
}
public Comparator SuccessBasedTreatmentComparator() {
// TODO Auto-generated method stub
class SuccessBasedComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Treatment t1=(Treatment)o1;
Treatment t2=(Treatment)o2;
if(t1.probabilityOfSuccess<t2.probabilityOfSuccess)
return -1;
else if(t1.probabilityOfSuccess>t2.probabilityOfSuccess)
return 1;
return 0;
}
}
return new SuccessBasedComparator();
}
}
class SuccessBasedComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
Treatment t1=(Treatment)o1;
Treatment t2=(Treatment)o2;
if(t1.probabilityOfSuccess<t2.probabilityOfSuccess)
return -1;
else if(t1.probabilityOfSuccess>t2.probabilityOfSuccess)
return 1;
return 0;
}
}
class PriorityQueue<T> implements Heap<T> {
private transient int size;
private transient ArrayList<T> queue;
private transient Comparator<T> comparator;
PriorityQueue(ArrayList<T> lst){
this.queue=lst;
}
public static <E> PriorityQueue<E> fromCollection(Collection<? extends E> c, Comparator<E> comparator) {
// TODO
ArrayList<E> lst=new ArrayList<E>(c);
Collections.sort(lst,comparator);
return new PriorityQueue(lst);
}
@Override
public int hashCode() {
return queue.hashCode();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public T findBest() {
// TODO Auto-generated method stub
return null;
}
@Override
public void insert(T t) {
// TODO Auto-generated method stub
}
@Override
public T deleteBest() {
// TODO Auto-generated method stub
return null;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.