K
Kevin Murray
I have been learning Java through the Core Java series. I am trying to
get one of the examples to work by splitting it up into packages. It
works fine except one thing. The equals overload is exhibiting a
strange behavior. It is the EqualsTest.java program in Chap. 5 of the
7th Ed. Here is what I have and I have truncated it slightly for
readability:
When carl.equals(boss) is called it behaves like I expect it to by
making the super call to the Employee.equals() method. However when
the alice1.equals(alice3) is called it does not execute the
Employee.equals() method.
Now the funky part is if I put all of these classes into one file and
remove the public statements from the Employee and Manager
declarations, the alice1.equals(alice3) calls the proper Employee
method. I am at a complete loss for why this happens. If someone could
help please emlighten me, it would be greatly appreciated. Thank you
in advanced.
EqualsTest.java
---------------------------------------------------
import com.horstmann.corejava.*;
import java.util.*;
import static java.lang.System.*;
public class EqualsTest {
public static void main (String[] args) {
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12,
15);
Employee alice2 = alice1;
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12,
15);
Employee bob = new Employee("Bob Brandson", 50000, 1989, 10,
1);
out.println("alice1.equals(alice3) : " + alice1.equals(alice3));
out.println("alice1.equals(bob) : " + alice1.equals(bob));
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);
out.println("carl.equals(boss) : " + carl.equals(boss));
}
}
-----------------------------------------
now my packages are:
Employee.java
-------------------------------------------
package com.horstmann.corejava;
import java.util.*;
public class Employee {
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month -
1, day);
hiredate = calendar.getTime();
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDate() {
return hiredate;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
public boolean equals(Object otherObject) {
System.out.println("\nequals() : this = " + this);
System.out.println( "equals() : otherObject = " + otherObject);
System.out.println();
if (this == otherObject)
return true;
if (otherObject == null)
return false;
if (getClass() != otherObject.getClass())
return false;
Employee other = (Employee) otherObject;
return ((name.equals(other.name)) &&
(salary == other.salary) &&
(hiredate.equals(other.hiredate)));
}
public int hashCode() {
return 7 * name.hashCode() +
11 * new Double(salary).hashCode() +
13 * hiredate.hashCode();
}
public String toString() {
return getClass().getName() +
"[name = " + name +
", salary = " + salary +
", hiredate = " + hiredate +
"]";
}
private String name;
private double salary;
private Date hiredate;
}
------------------------------------------------
and the Manager.java
--------------------------------------------------
package com.horstmann.corejava;
public class Manager extends Employee {
public Manager(String n, double s, int year, int month, int day) {
super(n, s, year, month, day);
bonus = 0;
}
public double getSalary() {
double baseSalary = super.getSalary();
return (baseSalary + bonus);
}
public void setBonus(double b) {
bonus = b;
}
public boolean equals(Object otherObject) {
if (!super.equals(otherObject))
return false;
Manager other = (Manager)otherObject;
return bonus == other.bonus;
}
public int hashCode() {
return super.hashCode() +
17 * new Double(bonus).hashCode();
}
public String toString() {
return super.toString() +
"[bonus = " + bonus +
"]";
}
private double bonus;
}
get one of the examples to work by splitting it up into packages. It
works fine except one thing. The equals overload is exhibiting a
strange behavior. It is the EqualsTest.java program in Chap. 5 of the
7th Ed. Here is what I have and I have truncated it slightly for
readability:
When carl.equals(boss) is called it behaves like I expect it to by
making the super call to the Employee.equals() method. However when
the alice1.equals(alice3) is called it does not execute the
Employee.equals() method.
Now the funky part is if I put all of these classes into one file and
remove the public statements from the Employee and Manager
declarations, the alice1.equals(alice3) calls the proper Employee
method. I am at a complete loss for why this happens. If someone could
help please emlighten me, it would be greatly appreciated. Thank you
in advanced.
EqualsTest.java
---------------------------------------------------
import com.horstmann.corejava.*;
import java.util.*;
import static java.lang.System.*;
public class EqualsTest {
public static void main (String[] args) {
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12,
15);
Employee alice2 = alice1;
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12,
15);
Employee bob = new Employee("Bob Brandson", 50000, 1989, 10,
1);
out.println("alice1.equals(alice3) : " + alice1.equals(alice3));
out.println("alice1.equals(bob) : " + alice1.equals(bob));
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
boss.setBonus(5000);
out.println("carl.equals(boss) : " + carl.equals(boss));
}
}
-----------------------------------------
now my packages are:
Employee.java
-------------------------------------------
package com.horstmann.corejava;
import java.util.*;
public class Employee {
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month -
1, day);
hiredate = calendar.getTime();
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDate() {
return hiredate;
}
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
public boolean equals(Object otherObject) {
System.out.println("\nequals() : this = " + this);
System.out.println( "equals() : otherObject = " + otherObject);
System.out.println();
if (this == otherObject)
return true;
if (otherObject == null)
return false;
if (getClass() != otherObject.getClass())
return false;
Employee other = (Employee) otherObject;
return ((name.equals(other.name)) &&
(salary == other.salary) &&
(hiredate.equals(other.hiredate)));
}
public int hashCode() {
return 7 * name.hashCode() +
11 * new Double(salary).hashCode() +
13 * hiredate.hashCode();
}
public String toString() {
return getClass().getName() +
"[name = " + name +
", salary = " + salary +
", hiredate = " + hiredate +
"]";
}
private String name;
private double salary;
private Date hiredate;
}
------------------------------------------------
and the Manager.java
--------------------------------------------------
package com.horstmann.corejava;
public class Manager extends Employee {
public Manager(String n, double s, int year, int month, int day) {
super(n, s, year, month, day);
bonus = 0;
}
public double getSalary() {
double baseSalary = super.getSalary();
return (baseSalary + bonus);
}
public void setBonus(double b) {
bonus = b;
}
public boolean equals(Object otherObject) {
if (!super.equals(otherObject))
return false;
Manager other = (Manager)otherObject;
return bonus == other.bonus;
}
public int hashCode() {
return super.hashCode() +
17 * new Double(bonus).hashCode();
}
public String toString() {
return super.toString() +
"[bonus = " + bonus +
"]";
}
private double bonus;
}