public class Person { private String firstName; private String lastName; public
ID: 3641220 • Letter: P
Question
public class Person{
private String firstName;
private String lastName;
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;
}
}
a.) The above code defines the class Person to store the name of a person. The methods that we included merely set the name and print the name of a person. Redefine the class Person so that, in addition to what the existing class does, you can:
i. Set the last name only.
ii. Set the first name only.
iii. Set the middle name.
iv. Check whether a given last name is the same as the last name of this person.
v. Check whether a given first name is the same as the first name of this person.
vi. Check whether a given middle name is the same as the middle name of this person.
b.) Add the method equals that returns true if two objects contain the same first and last name.
c.) Add the method makeCopy that copies the instance variables of a Person object into another Person object.
d.) Add the method getCopy that creates and returns the address of the object, which is a copy of another Person object.
e.) Add the copy constructor.
f.) Write the definitions of the methods of the class Person to implement the operations for this class.
g.) Write a program that tests various operations of the class Person.
In java
Explanation / Answer
package util; 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; } public static void main(String args[]) { Person p1 = new Person("Sumitha", "Sudhakar"); Person p2 = new Person("Sumitha", "Sudhakar"); System.out.println("p1.equals(p2) result is : " + p1.equals(p2)); Person clonePerson = p1.makeCopy(); System.out.println("Using make Copy is firstName:" + clonePerson.getFirstName() + " " + clonePerson.getLastName()); Person p4 = new Person(p1); System.out.println("using copy constructor firstname :" + p4.getFirstName()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.