Solve Java (Object Oriented Programming) Solve Question 4 from your textbook in
ID: 3597602 • Letter: S
Question
Solve Java (Object Oriented Programming)
Solve Question 4 from your textbook in Malik 548, Chapter 8: User-Defined Classes and ADTs a. Example 8-8 defined 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 . Check whether a given first name is the same as the first name of Check whether a given middle name is the same as the middle b. Add the method equals that returns true if two objects contain the c Add the method makeCopy that copies the instance variables ofa d. Add the method getCopy that creates and returns the address of the person th is person. name of this person same first, middle, and last name. Person object into another Person object. 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 Write a program that tests various operations of the class PersonExplanation / Answer
public class Person {
private String firstName;
private String middleName;
private String lastName;
public Person() {
}
public Person(String firstName, String middleName, String lastName) {
super();
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean checkingLastName(String lName){
return this.lastName.equals(lName);
}
public boolean checkingFirsttName(String fName){
return this.firstName.equals(fName);
}
public boolean checkingMiddleName(String mName){
return this.middleName.equals(mName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (middleName == null) {
if (other.middleName != null)
return false;
} else if (!middleName.equals(other.middleName))
return false;
return true;
}
public void makeCopy(Person p){
this.firstName=p.firstName;
this.middleName=p.middleName;
this.lastName=p.lastName;
}
public Person getCopy(){
return this;
}
public Person(Person p){
this.firstName=p.firstName;
this.middleName=p.middleName;
this.lastName=p.lastName;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", middleName=" + middleName + ", lastName=" + lastName + "]";
}
public static void main(String[] args) {
Person p=new Person();
p.setFirstName("Paruchuri");
p.setMiddleName("Tarun");
p.setLastName("Teja");
Person p1=new Person();
p1.setFirstName("Paruchuri");
p1.setMiddleName("Tarun");
p1.setLastName("Teja");
System.out.println(p.checkingFirsttName("Paruchuri"));
System.out.println(p.checkingLastName("Teja"));
Person p2=new Person("Jai","Lava","Kusha");
p1.makeCopy(p2);
System.out.println(p1);
System.out.println(p1.getCopy());
}
}
/* OUTPUT:-
true
true
Person [firstName=Jai, middleName=Lava, lastName=Kusha]
Person [firstName=Jai, middleName=Lava, lastName=Kusha]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.