这里的典型学生,为作业的最后步骤寻求一些意见。非常感谢任何指导我正确方向的帮助或提示。
我们的部分任务是实施急诊室PriorityQueue。我坚持的部分细节如下:
编写一个名为PatientQueue的课程:
一个。默认值,无参数 构造
湾两种公共方法:
一世。 public void registerPatient(Patient p)
II。 public Patient getNextPatient()℃。内部使用PriorityQueue和PatientComparator
这是我到目前为止所拥有的:
在Patient.java中:
package blah;
import java.util.Date;
public class Patient {
// data fields
protected String name;
protected int category;
protected Date timeArrived;
// accessors and mutators
public String getName() {
return name;
}
public void setName(String nameIn) {
this.name = nameIn;
}
public int getCategory() {
return category;
}
public void setCategory(int categoryIn) {
this.category = categoryIn;
}
public java.util.Date getTimeArrived() {
return timeArrived;
}
// default constructor
public Patient() {
this.name = "Default Name";
this.category = 5; // unclassified Patients go to the end of the queue
this.timeArrived = new Date();
}
// overloaded constructor
public Patient(String nameIn, int categoryIn) {
this.name = nameIn;
this.category = categoryIn;
this.timeArrived = new Date();
}
} // end Patient class
在PatientComparator.java中:
package blah;
import java.util.Comparator;
public class PatientComparator implements Comparator<Patient> {
public int compare(Patient p1, Patient p2) {
if (p1.getCategory() < p2.getCategory())
return -1;
if (p1.getCategory() > p2.getCategory())
return 1;
else { if (p1.getTimeArrived().before(p2.getTimeArrived()))
return -1;
if (p1.getTimeArrived().after(p2.getTimeArrived()))
return 1;
}
return 0;
}
} // end PatientComparator class
在PatientQueue.java中:
package blah;
import java.util.Comparator;
import java.util.PriorityQueue;
public class PatientQueue extends PriorityQueue<Patient> {
// default constructor
public PatientQueue() {
}
public void registerPatient(Patient p) {
//NEED HELP IN THIS PART//
} // end registerPatient method
public Patient getNextPatient() {
return (Patient)this.poll();
} // end getNextPatient method
} // end PatientQueue class
最后,在驱动程序EmergencyRoomSimulator.java中: 包裹
import java.util.Random;
public class EmergencyRoomSimulator {
private static final int WAIT_LIMIT = 3000; // 1000 = 1 second
private PatientQueue pq = new PatientQueue();
private void t() {
try { Thread.sleep(new Random().nextInt(WAIT_LIMIT));
} catch (InterruptedException e) {
}
} // end t method
private void patientArrives(Patient p) {
pq.registerPatient(p);
System.out.println(" ARRIVAL: " + p.getName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------");
t();
} // end patientArrives method
private void doctorVisits() {
Patient p = pq.getNextPatient();
System.out.println(" VISIT: " + p.getName());
System.out.println(" time arrived: " + p.getTimeArrived());
System.out.println(" category: " + p.getCategory());
System.out.println("------------------------------------");
t();
} // end doctorVisits method
private void simulate() {
System.out.println("------------------------------------");
System.out.println("ER OPEN");
System.out.println("------------------------------------");
patientArrives(new Patient("John Paul Jones", 3));
patientArrives(new Patient("Thomas Paine", 1));
patientArrives(new Patient("Joseph Brant", 2));
doctorVisits();
patientArrives(new Patient("Ethan Allen", 2));
patientArrives(new Patient("Henry Knox", 4));
patientArrives(new Patient("Patrick Henry", 2));
doctorVisits();
doctorVisits();
patientArrives(new Patient("Mary Draper", 1));
patientArrives(new Patient("Samuel Adams", 3));
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
doctorVisits();
System.out.println("------------------------------------");
System.out.println("ER CLOSED");
System.out.println("------------------------------------");
} // end simulate method
public static void main(String[] args) {
EmergencyRoomSimulator er = new EmergencyRoomSimulator();
er.simulate();
} // end main
} // end class
我得到的错误是:
线程“main”中的异常java.lang.NullPointerException
在主要的第一次doctorVisits()
通话中。我知道我错过了在列表中实际添加和/或删除对象的正确方法,但是我无法看到PatientQueue
类中需要发生什么来实际触发{{1}用于添加或“获取”下一位患者。
同样,非常感谢任何建议。谢谢!
答案 0 :(得分:1)
您的PatientQueue
未使用PatientComparator
。你应该使用超类的构造函数来注册它。
您的PatientQueue
不会将Patient
个对象添加到队列中。在registerPatient
方法中添加它们。
答案 1 :(得分:1)
我甚至不会单独制作一个比较器。您可以在Patient类上实现Comparable接口并实现必要的compareTo方法。 快速实施:
@Override
public int compareTo(Object o) {
Patient p = (Patient) o;
if (this.getCategory() < p.getCategory())
return -1;
if (this.getCategory() > p.getCategory())
return 1;
else { if (this.getTimeArrived().before(p.getTimeArrived()))
return -1;
if (this.getTimeArrived().after(p.getTimeArrived()))
return 1;
}
return 0;
}
还在registerPatient方法中添加this.add(p)
。
没有其他任何东西可以让程序运行。有关Comparable接口的更多信息:http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
编辑//
在您发表评论后,我更仔细地查看了作业,实际上它说:
℃。内部使用PriorityQueue和PatientComparator
这意味着(至少我会如何阅读)您实际上不必扩展PriorityQueue,但您可以将其用作内部资源。以下是我将如何实现PatientQueue类:
import java.util.Comparator;
import java.util.PriorityQueue;
public class PatientQueue {
PriorityQueue pq;
// default constructor
public PatientQueue() {
this.pq = new PriorityQueue<Patient>(1, new PatientComparator());
}
public void registerPatient(Patient p) {
this.pq.add(p);
} // end registerPatient method
public Patient getNextPatient() {
return (Patient) this.pq.poll();
} // end getNextPatient method
} // end PatientQueue class
并在您的EmergencyRoomSimulator类中将字段声明更改为
PatientQueue pq = new PatientQueue();