0

I have an array of people. I have instantiated the subclass Employee twice and one Student subclass in the array. I just want to know why I'm getting false when it should say true with regards to my equals method. Most of this code is from the Core Java Volume 1 Fundamentals and I'm reading through the book to get a refresher but have no clue what to do with the equals method. Is it because it's a Person Class. First I tried putting the putting a different name and salary on the 3 person in the Person Array. I was still getting false. And then I switch it to the same name and still got false. I don't always understand the debugging tool but I thought it looked like the problem was with my return statement.

package v1ch05.abstractClasses;


public class PersonTest
{
   public static void main(String[] args)
   {
      var people = new Person[3];

      // fill the people array with Student and Employee objects
      people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      people[1] = new Student("Maria Morris", "computer science");
      people[2] = new Employee("Harry Hacker", 50000, 1989, 10, 1);

      // print out names and descriptions of all Person objects
      for (Person p : people)
         System.out.println(p.getName() + ", " + p.getDescription());
      
      boolean isEmployee = people[0].equals(people[2]);
      System.out.println("Is Employee:" + isEmployee);
   }
   

}
package v1ch05.abstractClasses;

import java.time.*;
import java.util.Objects;

public class Employee extends Person
{
   private double salary;
   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)
   {
      super(name);
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public String getDescription()
   {
      return String.format("an employee with a salary of $%.2f", salary);
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
   
   @Override
   public boolean equals(Object otherObject) {
       if(this == otherObject) return true;
       
       if(otherObject == null) return false;
       
       if(getClass() != otherObject.getClass()) return false;
       
       Employee other = (Employee) otherObject;
       
       return super.equals(other)
               && salary == other.salary
               && Objects.equals(hireDay, other.hireDay);
   }
}
package v1ch05.abstractClasses;

public abstract class Person
{
   public abstract String getDescription();
   private String name;

   public Person(String name)
   {
      this.name = name;
   }

   public String getName()
   {
      return name;
   }
   
   @Override
   public boolean equals(Object otherObject) {
       if(this == otherObject) return true;
       
       if(otherObject == null) return false;
       
       if(getClass() != otherObject.getClass()) return false;
       
       Person other = (Person) otherObject;
       
       return name == other.name;
   }
}
mik3210
  • 1
  • 1
  • 5
  • Your 'equals' implementation has 3 clauses. Determine which one is reporting false. (My money's on the first one, since you have not provided the code of Person.equals for us to inspect). – dangling else May 22 '22 at 18:52
  • dangling else are you saying i need to override the equals function in the Person class – mik3210 May 22 '22 at 19:02
  • 1
    what is `super.equals(other)` doing? We cannot see the code :-/ – user16320675 May 22 '22 at 19:10
  • Do **you** need to override Person.equals()? No idea, since I don't know what is there already - you did not post that. But **someone** needs to have written code that determines equality for Persons, presumably on the basis of equality for names. And as mentioned in the previous comment, you're calling Person.equals(), so you'd better find out whether it does what you're expecting it to do. – dangling else May 22 '22 at 21:48
  • With your suggestion @danglingelse I solved the problemm by overriding the Person.equals() method. I've also added the Person class to this page for future reference – mik3210 May 22 '22 at 22:36
  • Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Johannes Kuhn May 22 '22 at 22:53
  • `name == other.name` - `name` is a `String`. See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) to fix this mistake. – Johannes Kuhn May 22 '22 at 22:55
  • When I change the data in array[2] am I supposed to get false. – mik3210 May 22 '22 at 23:44

0 Answers0