Beginner Package Help Please

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;
}
 
S

Sudsy

Kevin Murray wrote:
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.
<snip>

I cut and pasted your code and, apart from the "import static" statement
[perhaps new to 1.5?], it all works as expected. You might be running
into problems with old .class files in your classpath. This is not at
all uncommon.
 
K

Kevin Murray

Sudsy said:
I cut and pasted your code and, apart from the "import static" statement
[perhaps new to 1.5?], it all works as expected. You might be running
into problems with old .class files in your classpath. This is not at
all uncommon.

Yes, I am running 1.5. As for the class path, I use the -cp option
when executing javac and the path I use with that option is . (the
current directory). I am not using any IDE, just compiling and running
from the command line. I am doing this in the Linux environment,
though I doubt that would be any issue, then again being new to java I
wouldn't know at this point and time.
 
A

Andrew Thompson

Sudsy said:
I cut and pasted your code and, apart from the "import static" statement
[perhaps new to 1.5?], it all works as expected. You might be running
into problems with old .class files in your classpath. This is not at
all uncommon.

To check what Sudsy mentioned, simply delete
all the class files and recompile them.
Yes, I am running 1.5. As for the class path, I use the -cp option
when executing javac..

Are you 'compiling using javac' or
'executing using java/javaw'?

<http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html#options>
Note that javac has no -cp option,
it has the -classpath option.

And to close, I feel you might get
good value from a different group
at the moment, described here...
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top