Here is a Java coding problem about hashing. Thumbs up if your answer works and
ID: 3827963 • Letter: H
Question
Here is a Java coding problem about hashing. Thumbs up if your answer works and appreciate your help! (c) Hashing. 7 points. An instance of class CMSGroup below maintains information about two stu- dents who grouped for an assignment on the CMS. We include only information that is necessary for this question. Class Student, among other things, has its own functions equals and hashCode. We expect some program written by the instructors to use a hash set of objects of CMSGroup to maintain information about grouping, so class CMSGroup needs functions equals and hashCode. Complete r. (2) Choose a reasonable but simple hashCode; it could be based on the hashCodes of the students.Explanation / Answer
HI,Please find my simple implementation.
Please let me know in case of any issue.
public class CMSGroup {
Student s1;
Student s2;
@Override
public boolean equals(Object obj) {
if(obj instanceof CMSGroup){
CMSGroup g = (CMSGroup)obj;
if(s1.equals(g.s1) && s2.equals(g.s2))
return true;
else if(s1.equals(g.s2) && s2.equals(g.s1))
return true;
else if(s2.equals(g.s1) && s1.equals(g.s2))
return true;
else
return false;
}
return false;
}
@Override
public int hashCode() {
return s1.hashCode()+s2.hashCode();
}
}
class Student{
private String firstName;
private String lastName;
public Student() {
}
// other members
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.