我有这个功课,我必须完成,但接下来的事情真的让我烦恼。我一直在考虑这个问题,但我认为我的方法存在缺陷。好吧,它确实是,因为我尝试过,它只是给了我一个stackoverflow异常。无论如何,这是指导者给我的规格片段:
- 一个部分被定义为教师,学科和教师的组合 时间表。
- 一个部分最多可容纳四十(40)名学生。
- 每个科目值三(3)个单位。老师被识别出来 他/她的教师ID。
- 教师不能用相同的时间表教授两个部分。
我的老师班有他/她教的部分列表。我计划如果我创建一个部分,构造函数必须首先检查该部分的计划是否与列表中的计划冲突。如果没有冲突,则成功创建该部分,并且必须将该部分添加到教师具有的部分列表中。我想我的逻辑还可以,但我的实现不是!
以下是我根据上述说明创建的课程:
import java.util.List;
import org.apache.commons.lang3.Validate;
public class Section {
//A section is defined as a combination of a teacher, a subject and a schedule.
private Teacher teacher;
private Subject subject;
private Schedule schedule;
private String sectionName;
//A section can accommodate a maximum of forty (40) students.
private int numOfStudentsItCanAccomodate = 40;
public Section(Teacher teacher, Subject subject, Schedule schedule,
String sectionName){
this.teacher = teacher;
this.subject = subject;
this.schedule = schedule;
this.sectionName = sectionName;
Validate.notNull(teacher,"Teacher field"+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notNull(subject,"Subject field"+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notNull(schedule,"Schedule field"+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notNull(sectionName,"Section name"+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notBlank(sectionName,"Section name"+Prompts.FAILED_PROMPT_BLANK.getMessage());
if(sectionConflictsWithTeachersOtherSections(teacher,schedule)==true){
throw new IllegalArgumentException(Prompts.FAILED_PROMPT_TEACHER.getMessage());
}else{
//**I believe this line faulty** No! It is faulty and stupid of me. T_T
teacher.addSection(new Section (teacher,subject,schedule,sectionName));
}
}
public Teacher getTeacher() {
return teacher;
}
public Subject getSubject() {
return subject;
}
public Schedule getSchedule() {
return schedule;
}
public String getSectionName() {
return sectionName;
}
//A teacher cannot teach two sections with the same schedule.
private boolean sectionConflictsWithTeachersOtherSections(Teacher teacher,Schedule newSchedule){
boolean conflict = false;
List<Section> teachersListOfSections = teacher.getListOfSections();
if(teacher.getListOfSections().size()>0){
for(Section takenSection:teachersListOfSections){
Schedule scheduleOfTakenSection = takenSection.getSchedule();
if(scheduleOfTakenSection.getDay().equals(newSchedule.getDay()) &&
scheduleOfTakenSection.getTime().equals(newSchedule.getTime())){
conflict = true;
}
}
}
return conflict;
}
public void subtractNumOfStudentsItCanAccomodate(){
this.numOfStudentsItCanAccomodate--;
}
public int getNumOfStudentsItCanAccomodate() {
return numOfStudentsItCanAccomodate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + numOfStudentsItCanAccomodate;
result = prime * result
+ ((schedule == null) ? 0 : schedule.hashCode());
result = prime * result
+ ((sectionName == null) ? 0 : sectionName.hashCode());
result = prime * result + ((subject == null) ? 0 : subject.hashCode());
result = prime * result + ((teacher == null) ? 0 : teacher.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Section other = (Section) obj;
if (numOfStudentsItCanAccomodate != other.numOfStudentsItCanAccomodate)
return false;
if (schedule == null) {
if (other.schedule != null)
return false;
} else if (!schedule.equals(other.schedule))
return false;
if (sectionName == null) {
if (other.sectionName != null)
return false;
} else if (!sectionName.equals(other.sectionName))
return false;
if (subject == null) {
if (other.subject != null)
return false;
} else if (!subject.equals(other.subject))
return false;
if (teacher == null) {
if (other.teacher != null)
return false;
} else if (!teacher.equals(other.teacher))
return false;
return true;
}
public boolean conflictsDayWith(Section newSection){
return (this.schedule.getDay().equals(newSection.schedule.getDay()));
}
public boolean conflictsTimeWith(Section newSection){
return (this.schedule.getTime().equals(newSection.schedule.getTime()));
}
}
我的老师班:
public class Teacher extends Person{
private List<Section> listOfSections;
public Teacher(String firstName,String lastName, String middleName)
throws IllegalArgumentException{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.listOfSections = new ArrayList<Section>();
Validate.notNull(firstName, "First name "+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notNull(lastName, "Last name"+Prompts.FAILED_PROMPT_NULL.getMessage());
Validate.notNull(middleName, "Middle name"+Prompts.FAILED_PROMPT_NULL.getMessage());
}
public void addSectionToList(Section newSection){
listOfSections.add(newSection);
}
public void teachesSection(Section newSection){
this.listOfSections.add(newSection);
}
public List<Section> getListOfSections() {
return listOfSections;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((listOfSections == null) ? 0 : listOfSections.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Teacher other = (Teacher) obj;
if (listOfSections == null) {
if (other.listOfSections != null)
return false;
} else if (!listOfSections.equals(other.listOfSections))
return false;
return true;
}
@Override
public String toString(){
return firstName + " " + middleName + " " + lastName ;
}
}
答案 0 :(得分:1)
您在哪一行导致StackoverflowException是正确的。这一行:
teacher.addSection(new Section (teacher,subject,schedule,sectionName));
导致一个infinte循环,因为你传入相同的参数,每次都会发生同样的事情。根据Dilbert的说法,在确切的情况下重复同样的事情并期待不同的结果是疯狂的表现:)
将其替换为:
teacher.addSection(this);