这里的典型学生,为作业的最后步骤寻求一些输入 . 任何帮助或提示指出我正确的方向非常感谢 .
我们的部分任务是实施急诊室PriorityQueue . 我坚持的部分细节如下:
写一个名为PatientQueue的课程:a . 默认的no-arg构造函数b . 两种公共方法:i . public void registerPatient(Patient p)ii . public Patient getNextPatient()c . 内部使用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中:package blah;
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()
电话 . 我知道'm missing the proper way to actually add and/or remove an object to/from the list, but I can't看到 PatientQueue
类需要发生什么来实际触发 PriorityQueue
被用来添加或"get"下一个病人 .
再次,非常感谢任何建议 . 谢谢!
2 回答
我甚至不会单独制作一个比较器 . 您可以在Patient类上实现Comparable接口并实现必要的compareTo方法 . 快速实施:
还要在registerPatient方法中添加
this.add(p)
.没有其他任何东西可以让程序运行 . 有关Comparable接口的更多信息:http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
编辑//
在您发表评论后,我更仔细地查看了作业,实际上它说:
这意味着(至少我会如何阅读)您实际上不必扩展PriorityQueue,但您可以将其用作内部资源 . 以下是我将如何实现PatientQueue类:
在您的EmergencyRoomSimulator类中,只需将字段声明更改为
PatientQueue pq = new PatientQueue();
您的
PatientQueue
不使用PatientComparator
. 您应该使用超类的构造函数来注册它 .并且
PatientQueue
不会将Patient
对象添加到队列中 . 在registerPatient
方法中添加它们 .