Java already has the .equals() method in its library, but it compares the hex value of the objects rather than the information inside, so we have to rewrite the method
public class studentMain{
public static void main(String[]args){
Student s1 = new Student("Alex"); //instance of the class
Student s2 = new Student("Bert"); //takes name as parameter
Student s3 = new Student("Alex");
//always prints false because it is comparing the hex number
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
//after coding the .equals() method
System.out.println(s1.equals(s3));
//prints true if the age and name are the same
}
}
//new file (object)
public class Student{
//instance variables
private String name;
private int age;
//Constructor(s)
//Accessor, modifier and helper methods
public boolean equals(Object a){
Student s = (Student)a; //cast to be object type that we want (s2)
if(name.equals(s.getName()) && age == s.getAge()){
return true;
}
return false;
}
}