I have a class Person: public class Person { private String firstName; private S
ID: 3641388 • Letter: I
Question
I have a class Person:public class Person
{
private String firstName;
private String lastName;
private String middleName;
public Person(Person p)
{
firstName = p.getFirstName();
lastName = p.getLastName();
}
public Person()
{
firstName = " ";
lastName = " ";
}
public Person(String first, String last)
{
setName(first, last);
}
public String toString()
{
return (firstName + " " + lastName);
}
public void setName(String first, String last)
{
firstName = first;
lastName = last;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getMiddleName()
{
return middleName;
}
public void setMiddleName(String middleName)
{
this.middleName = middleName;
}
public void checkFirstName(String firstName, Person person)
{
if (firstName.equalsIgnoreCase(person.getFirstName()))
{
System.out.println("First names are equal");
}
}
public void checkMiddleName(String middleName, Person person)
{
if (middleName.equalsIgnoreCase(person.getMiddleName()))
{
System.out.println("Middle names are equal");
}
}
public void checkLastName(String lastName, Person person)
{
if (lastName.equalsIgnoreCase(person.getLastName()))
{
System.out.println("Last names are equal");
}
}
public boolean equals(Person p2)
{
boolean flag = false;
if ((this.getFirstName().equals(p2.getFirstName())) && (this.getLastName().equals(p2.getLastName())))
{
flag = true;
}
return flag;
}
public Person makeCopy()
{
Person personCopy = null;
personCopy = new Person(this.getFirstName(), this.getLastName());
return personCopy;
}
}
I'm having trouble writing a program that allows the user to test various operations of the class Person.
Explanation / Answer
public class PersonTest { public static void main(String[] args) { Person a = new Person("John", "Smith"); Person b = new Person(a); Person c = new Person(); System.out.println("Person 1: " + a.toString()); System.out.println("Person 2: " + b.toString()); c.setName("Joe", "Schmoe"); System.out.println("Person 3: " + c.toString()); System.out.println("Person 1's first name: " + a.getFirstName()); System.out.println("Person 1's last name: " + a.getLastName()); a.setFirstName("Jane"); System.out.println("Person 1: " + a.toString()); a.setLastName("Kennedy"); System.out.println("Person 1: " + a.toString()); a.setMiddleName("Austin"); System.out.println("Person 1's middle name: " + a.getMiddleName()); a.checkFirstName("Jane", a); a.checkFirstName("Joe", a); a.checkMiddleName("Austin", a); a.checkMiddleName("Samantha", a); a.checkLastName("Kennedy", a); a.checkLastName("Smith", a); if(a.equals(b)) { System.out.println("The two people are equal"); } Person d = new Person(a.makeCopy()); if(a.equals(d)) { System.out.println("The two people are equal"); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.