I have to create a course with some undergraduate and postgraduate students, then extract from the course all postgraduate students with “Ismael Bento” as their supervisor using the method getPostgraduates() and use the class Notifier to send them a message(print the text and recipient). However, nothing gets printed... My guess is that there is something wrong with my getPostgraduates() method.
Here is the main method:
package students;
import java.util.*;
public class ProgrammingTest {
public static void main (String[] args){
Academic rr = new Academic("Ricardo Rodriguez");
Academic ib = new Academic("Ismael Bento");
Set<Student> students = new HashSet<Student>();
Undergraduate ug1 = new Undergraduate("gg4", "Greg","gg4@", rr);
Undergraduate ug2 = new Undergraduate("pr3", "Pete","pr3@", ib);
Postgraduate pg1 = new Postgraduate("te2", "Ted", "te2@", rr);
Postgraduate pg2 = new Postgraduate("yj34", "Yao", "yj34@", ib);
Postgraduate pg3 = new Postgraduate("jj8", "Jack", "jj8@", ib);
students.add(ug1);
students.add(ug2);
students.add(pg1);
students.add(pg2);
students.add(pg3);
Course c1 = new Course("c1", students);
Set<? extends Notifiable> n = c1.getPostgraduates("Ismael Bento");
Notifier notifier = new Notifier(n);
notifier.doNotifyAll("You have been notified!");
}
}
and the course class:
package students;
import java.util.*;
public class Course {
private Set<Student> students;
private String name;
public Course (String name, Set<Student> students){
this.name = name;
this.students = students;
}
public Set<Postgraduate> getPostgraduates(String nameOfSupervisor){
Set<Postgraduate> postgraduates = new HashSet<Postgraduate>();
for(Postgraduate p : postgraduates) {
if (p.getSupervisor().equals(nameOfSupervisor)){
postgraduates.add(p);
}
}
return postgraduates;
}
}
and the notifier class:
package students;
import java.util.Iterator;
import java.util.Set;
public class Notifier {
Set<? extends Notifiable> notifiables;
public Notifier (Set<? extends Notifiable> n) {
notifiables = n;
}
public void doNotifyAll(String message) {
Iterator<? extends Notifiable> i = notifiables.iterator();
while(i.hasNext()){
i.next().notify();
}
}
}
The problem is indeed in getPostgraduates()
. This reason for this is that you only ever compare against the object postgraduate
, which is initialized with null-values. You should run through the whole set of students, and check for postgraduates with the supervisor you are looking for instead.
you are comparing objects by == sign and using this condition to decide whether to add in set or not.. which will always false as it compares references and not actual objects.. override equals if you would like to compare the objects.
if(postgraduate.getSupervisor() == new Academic(nameOfSupervisor)){
postgraduates.add(postgraduate);
}
if you would like to compare the objects then you should call equals and override the equals.
Also HashSet implementation relies on equals and hashcode to determine the equality.. have you override the equals and hashcode in Postgraduate?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With