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

java question Review the Student class provided with the files for this homework

ID: 3680010 • Letter: J

Question

java question

Review the Student class provided with the files for this homework.Override the equals method and the hashCode method for this class.

Note: Make sure you're using the homework version of student (with firstName, lastName, tuitionPaid, and gpa). Do not use the version from the lecture notes, which has fewer variables.

Write both methods, including the complete method headers.

Make sure your methods satisfy the equals and hashCode contracts, found here: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

I strongly recommend testing out your two methods, similar to what is done on the lecture videos.

Explanation / Answer

public class Student {
   String firstName;
   String lastName;
   double tuitionPaid;
   String gpa;

   /**
   * @param firstName
   * @param lastName
   * @param tuitionPaid
   * @param gpa
   */
   public Student(String firstName, String lastName, double tuitionPaid,
           String gpa) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.tuitionPaid = tuitionPaid;
       this.gpa = gpa;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#equals(java.lang.Object)
   */
   @Override
   public boolean equals(Object obj) {
       // TODO Auto-generated method stub
       if (obj == null)
           return false;

       if (!(obj instanceof Student))
           return false;

       Student student = (Student) obj;
       if (this.firstName == student.firstName
               && this.lastName == student.lastName && this.gpa == student.gpa
               && this.tuitionPaid == student.tuitionPaid)
           return true;
       else
           return false;

   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#hashCode()
   */
   @Override
   public int hashCode() {
       // TODO Auto-generated method stub

       return (int) firstName.hashCode() * lastName.hashCode()
               * gpa.hashCode() * (int) tuitionPaid;
   }

   public static void main(String[] args) {

       try {
           Student student1 = new Student("Rajesh", "Kumar", 500, "A");
           Student student2 = new Student("Pavan", "Kumar", 500, "A");
           Student student3 = new Student("Rajesh", "Kumar", 500, "A");
           System.out.println("is student1 and student2 are equal?"
                   + student1.equals(student2));
           System.out.println("is student1 and student3 are equal?"
                   + student1.equals(student3));
           System.out.println("student1 hashcode:" + student1.hashCode());
           System.out.println("student2 hashcode:" + student2.hashCode());
           System.out.println("student3 hashcode:" + student3.hashCode());

       } catch (Exception e) {
           // TODO: handle exception
       }
   }

}

OUTPUT:

is student1 and student2 are equal?false
is student1 and student3 are equal?true
student1 hashcode:-125826832
student2 hashcode:1504807200
student3 hashcode:-125826832