Job interview question 1

By Robert Sayfullin

Q: Write equation function for class

public class Customer {
private int id;
private String name;
private float balance;
}

A1: What for? It is already implemented in Object class.
A2: Why me? My favourite IDE generates such staff for me.
A3: You think for a while and write classical equation code with null-checking, casting and equation of values of all fields.
A4: Easy job. Just put inside

return EqualsBuilder.reflectionEquals(this, other);

A5: Look mait! It seems to be a Business object! And field “id” looks like primary key for me! So it should be something like:

public boolean equals(Object other) {
if(this.id==null)
return false;
if (other==null)
return false;
if( !(other.getClass().equals(this.getClass())))
return false;
return id.equals((Customer)other.id);
}

Leave a Reply