Skip to main content
0 online
theremin May 3, 2006

Quite a lot to do with this one.. so Far I have a class that looks like public class Person { private String firstName; private String lastName; public Person() { firstName = ""...

iwz May 3, 2006

Well, here's something to get you started: "So how would I set the last name only" q[Person person = new Person(); person.setLastName("Smith");]q or q[Person person = new Person(null, "Smi...

theremin thereminOG 2002

That is kind. Thank you.

iwz iwz

here's a bonus equals method you can add to the Person class:

public boolean equals( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;

final Person person = (Person)o;

if ( lastName != null ? !lastName.equals( person.lastName ) : person.lastName != null ) return false;

return true;
}

theremin thereminOG 2002

Well this is what I did.. tell me if it looks right. It compiles without errors at least. hehe

public class Person
{
private String firstName;
private String middleName;
private String lastName;


public Person()
{
firstName = "";
middleName = "";
lastName = "";

}

public Person(String first, String middle, String last)
{
setName(first, middle, last);
}

public String toString()
{
return (firstName + " " + middleName + " " + lastName);
}

public void setName(String first, String middle, String last)
{
firstName = first;
middleName = middle;
lastName = last;
}

public Person setLastName(String last)
{
lastName = last;

return this;
}

public Person setMiddleName(String middle)
{
middleName = middle;

return this;
}

public Person setFirstName(String first)
{
firstName = first;

return this;
}

public String getFirstName()
{
return firstName;
}

public String getMiddleName()
{
return middleName;
}

public String getLastName()
{
return lastName;
}

public boolean equals(Person otherPerson)
{
return(firstName == otherPerson.firstName && lastName == otherPerson.lastName);
}

public void makeCopy(Person otherPerson)
{
firstName = otherPerson.firstName;
middleName = otherPerson.middleName;
lastName = otherPerson.lastName;
}

public Person getCopy()
{
Person temp = new Person();

temp.firstName = firstName;
temp.middleName = middleName;
temp.lastName = lastName;

return temp;
}
}

iwz iwz

Looks pretty good. I'd change a couple of things tho.

== is not proper for comparing Strings. use .equals() instead.

so this firstName == otherPerson.firstName && lastName == otherPerson.lastName should be like this firstName.equals( otherPerson.firstName) && lastName.equals(otherPerson.lastName)

Also, the makeCopy(Person) method may be more appropriate as a constructor instead of a method.

public Person( Person person )
{
firstName = person.firstName;
middleName = person.middleName;
lastName = person.lastName;
}

Also, for the getCopy method, take a look at the Cloneable interface, and the clone() method. It may give you a cleaner implementation. But that depends on your requirements, of course.

theremin thereminOG 2002

Oh, we didn't talk about a clone() method yet. Thanks for all the help as usual.

D
dgiaimoOG 2003

Your equals method shouldn't throw an Exception if you pass in null, as this one will.

deanh77 deanh77Founder

yes check that the otherPerson is null, and return false if it is. and use .equals on Strings like Ian said, since == is a reference equality in objects (i.e. these two variables point to the same object in memory), but String's equals() does an equality check on the contents of the Object (the internal array of characters that make up the String).

Welcome Back to eZabel

It's been a while. Here's what's new.

eZabel Lore

A complete history of our community — stats, Hall of Fame, legendary threads, and more.

View the Lore →

Everything Preserved

All 225,969 pieces of content from 2000–2014 are here — forums, messages, journals, photos, polls, and events.

💎

Gems

Spot something you love — a legendary comment, a classic thread, a great photo? Log in and click the diamond icon to mark it as a Gem. Add a note about why it's special. The best stuff surfaces on the Gems page.