Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the hashCode() function for the class given below. Ensure that the imp

ID: 3852836 • Letter: I

Question

Implement the hashCode() function for the class given below. Ensure that the implementation produces unique hash codes for the values given in the examples. Do not rely on the string representation of Student for its hashcode computation. class student. { String first, last: inl credits: char status: public Student (String f, String 1, int c, char s) (first = f: last = l: credits = c: 3tatus = s: } public int hashCode () { //YOUR CODE. HERE.] } Kxamp1es which should all have unique hash codes Student s1 = new Student ("Mohammed", "Ali", 24, 'A'), s2 = new student ("Al i ", "Mohammed", 2.4, 'A'), s3 = new Student ("Mohammed Ali", "", 24, 'A'), s4 = new student. ("George", "Foreman", 65, 'I'), s5 new Student ("George", "Foreman", 73, 'A'), s6 = new Student ("", ", 0, '');

Explanation / Answer

Below is your implementation: -


public class student {
   String first,last;
   int credits;
   char status;
   public student(String f, String l, int c, char s) {
       super();
       this.first = f;
       this.last = l;
       this.credits = c;
       this.status = s;
   }
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + credits;
       result = prime * result + ((first == null) ? 0 : first.hashCode());
       result = prime * result + ((last == null) ? 0 : last.hashCode());
       result = prime * result + status;
       return result;
   }

  
   public static void main(String[] args) {
       student s1 = new student("Muhammad", "Ali", 24, 'A');
       System.out.println(s1.hashCode());
       student s2 = new student("Ali", "Muhammad", 24, 'A');
       System.out.println(s2.hashCode());
       student s3 = new student("Muhammad Ali", "", 24, 'A');
       System.out.println(s3.hashCode());
       student s4 = new student("George", "Foreman", 65, 'I');
       System.out.println(s4.hashCode());
       student s5 = new student("George", "Foreman", 73, 'A');
       System.out.println(s5.hashCode());
       student s6 = new student("", "", 0, '');
       System.out.println(s6.hashCode());
   }
}

Sample Output: -

1014655984
1067429156
-510735988
-1845108502
-1844870182
923521

As hashcodes are different here, for all, So our hashcode function seems good