import java.util.Collections; import java.util.Iterator; import java.util.List; public class GraduateStudent extends Student implements Iterable { private List supervisors; public GraduateStudent(String name, List supervisors) { super(name); this.supervisors = supervisors; } /** * This method should probably throw something more specific * than just Exception. */ public void setSupervisors(List supervisors) throws Exception { if (supervisors.size() == 0) { throw new IllegalArgumentException("no supervisors!"); } this.supervisors = supervisors; } /** * This is the standard idiom for creating an array from a list. */ public String[] getSupervisors() { return this.supervisors.toArray(new String[0]); } public Iterator iterator() { return this.supervisors.iterator(); } public List getSortedSupervisors() { Collections.sort(this.supervisors); return this.supervisors; } /** * equals is a bit tricky. You must check the list * of sorted supervisors for equality because list * equals returns false if both lists contain the * same elements in different order. */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (getClass() != obj.getClass()) { return false; } GraduateStudent other = (GraduateStudent) obj; if (supervisors == null) { if (other.supervisors != null) { return false; } } else if (!this.getSortedSupervisors() .equals(other.getSortedSupervisors())) { return false; } return true; } @Override public String toString() { return super.toString() + '\t' + this.supervisors.size(); } }