Java Reference
In-Depth Information
private List<Student> tutees = new ArrayList<Student>();
Professor() {
// Do nothing — needed for serialization
}
Professor(String theName) {
super(theName);
}
public List<Student> getTutees () {
return tutees;
}
/**
* checkTutees checks that all the tutees
* have this Professor as their tutor
*/
public boolean checkTutees () {
boolean result = true;
for (Student stu: tutees) {
if (stu.getTutor() != this) {
result = false;
break;
}
}
return result;
}
}
// ...
Professor jane = new Professor("Jane");
Student able = new Student("Able", jane);
Student baker = new Student("Baker", jane);
Student charlie = new Student("Charlie", jane);
jane.getTutees().add(able);
jane.getTutees().add(baker);
jane.getTutees().add(charlie);
System.out.println("checkTutees returns: " +
jane.checkTutees());
// Prints "checkTutees returns: true"
Professor and Student are types that extend the basic type Person . A student (that
is, an object of type Student ) has a tutor of type Professor . A professor (that is, an ob-
ject of type Professor ) has a list (actually, an ArrayList ) of tutees (of type Student ).
Search WWH ::




Custom Search