public class Employee{
//fields
String name;
//the name of the employee
int
id;
//the id number
//constructors
public Employee(String
n, int i){
name = n;
id = i;
}
//the default constructor
public Employee(){
this(null, 0); //this
refers to the constructor above with the arguments null and 0
}
//other methods here
}//Employee
Employee e1 = new Employee("Sarah Michaels", 1234);
Employee e2 = new Employee("Sarah Michaels", 1234);
We would like to test if
e1 and e2 correspond field by field and for that we need
to implement an equals methodfor
the Employee class.
Code:
public boolean equals(Object
ob){
if (! ob instanceof Employee) return false;
//1
Employee e =
(Employee)ob;
//2
if (e.name.equals(name)&&e.id==id) return true; //3
//Alternatively we could code:
// if (e.name.equals(this.name)&&e.id==this.id) return true;
//this refers to the calling instance
return
false;
//4
}//equals
Note:
- the argument is of type Object, so as to make the method
more robust.
- in 1 we test if the argument is of type Employee
- in 2 we cast the argument, which we know is of
type Employee, to Employee
- in 3 we compare the name and id fields, name
is of type String, hence the .equals.
id is of type int, a primitive type, and we can use ==.
- if you are still around to execute 4, you should return
false, because
the fields don't match.
Self Test 1:
what would be the outcome of the test: true or false?
if (e1 == e2)...
myOut.println(temp.data); //here myOut refers
to a PrintWriter
//and temp.data refers to some instance of the Employee class
we need to convert the data,
contained in instances of our Employee class, to String.
If the instance that is
stored at temp.data belongs to a class, that has a toString method,
then the conversion
is done dynamically at runtime.
This is very elegant, since
we do not need to mention the Employee class inside the
data structure program,
which would be a terminal flaw in any general purpose data structure program.
Code:
public String toString(){
return name+", "+id+"\n";
}// toString
Note:
- this will return a String containing name and
id separated by a comma and a space,
and ending with a carriage return.
Code:
public
int compareTo(Object ob)throws ClassCastException{
if (!(ob instanceof Employee)) throw new ClassCastException();
Employee e = (Employee) ob;
if (id > e.id) return 1;
if (id == e.id) return 0;
return -1;
}//compareTo
Note:
- the
ClassCastException is part of the Java Api and does not have to be written
by you.
In order to make effective
use of the compareTo method, we must make the Employee class an
extension of the Comparable
interface.
The Comparable interface
is an abstract class of the java language, which does not implement
its only method, the compareTo
method. You do not need to code the Comparable class, it is already
done for you in the java
language. Here it is anyway:
public abstract interface
Comparable
{
public int compareTo(Object o);
}
Since the Employee class,
with the inclusion of the compareTo method,
implements the Comparable
interface, we should state that in the header line
of the Employee class:
public class Employee implements Comparable{...}
Then every time, a data structure
(e.g. a binary search tree) program requires its data objects to
be of type Comparable, Employee
qualifies and it can be used in conjunction with the data structure.
public int hashCode(){
return id + name.hashCode();
}