Well, here's something to get you started:
"So how would I set the last name only"
Person person = new Person();
person.setLastName("Smith");
or
Person person = new Person(null, "Smith");
"Check whether a given last name is the same as the last name of this person"
String lastName = "Smith";
Person person1 = new Person( "John", "Smith" );
Person person2 = new Person( "Jane", "Johnson" );
System.out.println( person1.getLastName.equals( lastName ) );
System.out.println( person2.getLastName.equals( lastName ) );
That is kind. Thank you.
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;
}
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;
}
}
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.
Oh, we didn't talk about a clone() method yet. Thanks for all the help as usual.
Your equals method shouldn't throw an Exception if you pass in null, as this one will.
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).