2
/* Student class with a constructor that initialises the name, gender, and degreeProgramme. */

public class Student {

    public static void main(String[] args){
        Student s1 = new Student("a", "b", "c", "d");
        System.out.println(s1.toString());

        s1.setName("Mary Jones");
        s1.setGender("female");
        s1.setStudentID("0564");;
        s1.setDegreeProgramme("History");


        Student s2 = new Student("Mary jones", "female", "0564", "History");
        Student s3 = new Student("Mary Jones", "female", "0564", "History");
        System.out.println(s1);
        System.out.println(s3);
        System.out.println(s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
    }

    private String name; /* instance variable */
    private String gender; /* instance variable */
    private String studentID; /* instance variable */
    private String degreeProgramme; /* instance variable */

    /* Student constructor that receives 4 parameters */
    public Student(String name, String gender, String studentID, String degreeProgramme){

        this.name = name; /* assigns name to instance variable name */
        this.gender = gender; /* assigns gender to instance variable gender */
        this.studentID = studentID; /* assigns studentID to instance variable studentID */
        this.degreeProgramme = degreeProgramme; /* assigns degreeProgramme to instance variable degreeProgramme */


    }
    /* method that returns the name of the student */
    public String getName(){
        return name;
    }

    /* method that returns the gender of the student */
    public String getGender(){
        return gender;
    }
    /* method that returns the degree programme of the student */
    public String getDegreeProgramme(){
        return degreeProgramme;
    }

    /* method that returns the student ID */
    public String getStudentID(){
        return studentID;
    }

    /* method that sets the name of the student */
    public void setName(String name){
        this.name = name;
    }
    /* method that sets the student ID */
    public void setStudentID(String studentID){
        this.studentID = studentID;
    }

    /* method that sets the gender of the student */
    public void setGender(String Gender){
        this.gender = Gender;
    }

    /* method that sets the degree programme of the student */
    public void setDegreeProgramme(String degreeProgramme){
        this.degreeProgramme = degreeProgramme;
    }

    /* method that returns the name, gender, and degree programme of the student */
    public String toString(){
        String studentInfo = "["+name+ ", " +gender+ ", ID: " +studentID+ ", " +degreeProgramme+"]" ;
        return studentInfo;
    }


} /* end class Student */

Can anyone tell me why the line "System.out.println(s1.equals(s3));" returns output "false" even though the output is identical? I've been trying to figure it out for 10 hours and can't seem to figure out why.

libik
  • 22,239
  • 9
  • 44
  • 87
RTC123
  • 25
  • 2
  • 1
    Possible duplicate of [whats the difference between ".equals" and "=="](http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and) – Saurabh Jhunjhunwala Oct 21 '15 at 13:12

3 Answers3

3

You need to @override and write your own equals method . Currently it is checking if the two objects are the same. You need to rewrite it to check if the two objects have the same output.

Alex
  • 627
  • 3
  • 12
  • 32
1

Currently, you are using the equals method inherited from the class java.lang.Object which is implemented as follows:

public boolean equals(Object obj) {
    return this == obj;
}

It checks if this and obj are the referencing the same instance. In your case, s1 and s3 are different instances of Student, hence s1.equals(s3) return false.

You can implement your own equals method to achiever your purpose, for example:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Student)) return false;

    Student student = (Student) o;

    if (name != null ? !name.equals(student.name) : student.name != null) return false;
    if (gender != null ? !gender.equals(student.gender) : student.gender != null) return false;
    if (studentID != null ? !studentID.equals(student.studentID) : student.studentID != null) return false;
    return !(degreeProgramme != null ? !degreeProgramme.equals(student.degreeProgramme) : student.degreeProgramme != null);
}
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Bon
  • 3,073
  • 5
  • 21
  • 40
0

Because equals behave differntly.

If you try this, it will be true :

String s1String = s1.toString();
String s3String = s3.toString();
s1String(equals(s3String));
//or just simply
s1.toString().equals(s3.toString());

As already pointed out, you can either change the equals method (which can be potentially dangerous, if you are not completely sure what are you doing and what can be the consequences) or you can write your own method for checking if the properties are same, which I would recommend.


In your Student class, you can add this

public boolean hasSameProperties(Student student){
  return student.toString().equals(this.toString());
}

Then in your main method, you can just write

System.out.println(s1.hasSameProperties(s2));

libik
  • 22,239
  • 9
  • 44
  • 87