Job interview question 1

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: