Solve Java (Object Oriented Programming) Solve Question 4 from your textbook in
ID: 3597603 • 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
Source Code :
import java.util.ArrayList;
//person class should implements cloneable
public class Person implements Cloneable {
private String firstName;
private String lastName;
private String middleName;
public Person(String firstName, String lastName, String middleName) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
}
// copy constructor
public Person(Person lPerson) {
this.firstName = lPerson.getFirstName();
this.lastName = lPerson.getLastName();
this.middleName = lPerson.getMiddleName();
}
// getters for firstname,lastname and middlename
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getMiddleName() {
return middleName;
}
// override hashcode and generate based on firstname,lastname and middlename
public int hashCode() {
// if first name or last name or middle name is null then get hashcode
// by calling super or else generate hashcode basedon 3 properies
if (this.getFirstName() == null || this.getLastName() == null || this.getMiddleName() == null) {
super.hashCode();
} else {
return this.getFirstName().hashCode() + this.getLastName().hashCode() + this.getMiddleName().hashCode();
}
return super.hashCode();
}
// Override equals to check equality of two objects
public boolean equals(Object obj) {
Person lPerson = null;
// if null return false
if (obj == null)
return false;
// if object is not instanc of Person retun false
if (!(obj instanceof Person)) {
return false;
} else {
// type cast to person object
lPerson = (Person) obj;
}
// check null check to avoid null pointer execption and check equality
// on 3 properties
return (this.getFirstName() != null && this.getFirstName().equals(lPerson.getFirstName()))
&& (this.getLastName() != null && this.getLastName().equals(lPerson.getLastName()))
&& (this.getMiddleName() != null && this.getMiddleName().equals(lPerson.getMiddleName()));
}
// get new object by makeCopy method
public Object makeCopy() throws CloneNotSupportedException {
return (Person) this.clone();
}
// get address of new object of person
public int getCopy() throws CloneNotSupportedException {
return makeCopy().hashCode();
}
// main method to test
public static void main(String[] args) throws CloneNotSupportedException {
Person lPerson1 = new Person("Manmad", "Kumar", "Reddy");
Person lPerson2 = new Person("Manmad", "Reddy", "Reddy");
Person lPerson3 = new Person("Manmad", "Kumar", "Reddy");
Person lPerson4 = new Person("Siva", "Rami", "Reddy");
Person lPerson5 = new Person("Reddy", "Rami", "Reddy");
Person lPerson6 = new Person(null, null, "Reddy");
// Case 1 verifying equals false,both lastname's are wrong
System.out.println(lPerson1.equals(lPerson2));
// Case 2 verifying equals true
System.out.println(lPerson1.equals(lPerson3));
// Case 3 verifying equals false,both objects
// firstname,lastname,middlename are differ
System.out.println(lPerson1.equals(lPerson4));
// Case 4 verifying equals false,both objects firstname and lastname are
// different
System.out.println(lPerson1.equals(lPerson5));
// Case 5 verifying equlas false,when firstname and lastname's are null
System.out.println(lPerson1.equals(lPerson6));
Person lPerson = new Person("Manmad", "Kumar", "Reddy");
Person lCopyConstructor = new Person(lPerson);
// verifying copy construtor and equlas functionality
System.out.println(lPerson.equals(lCopyConstructor));
// verfiying copy origional object by custom clone
Person clonePerson = (Person) lPerson.makeCopy();
System.out.println(lPerson.equals(clonePerson));
// verfiying address of cloned object and origional object address,
// should return false
System.out.println(lPerson.hashCode() == lPerson.makeCopy().hashCode());
}
}
Hopefully! i have provide the solution for your problem please comment if you wish to know any more information or else please like it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.