Using Java: Read the class Person to store the name a person we discuss in the c
ID: 3590627 • Letter: U
Question
Using Java: Read the class Person to store the name a person we discuss in the class (source is Person.java attached in the assignment). As we can see that the methods we included merely set the name and print the name of a person. Redefine the class person so that, in addition to what existing Class does, you can
-Set the last name only.
-Set the first name only.
-Set the middle name.
-Check whether a given last name is the same as the last name of this person.
-Check whether a give first name is the same as the first name of this person.
-Check whether a given middle name is the same as the middle name of this person.
Add the method equals that returns true if two objects contains the same first and last name.
Add the method makeCopy that copies the instance of variables of a Person object into another Person object.
Add the method getCopy that creates and returns the address of the object, which is a copy of another Person Object.
Write the definition of the methods of the class Person to implement the operation of this Class
(Write a program that tests various operation of the Class Person.)
(Person.Java)
Explanation / Answer
public class Person
{
private String firstName; //store the first name
private String lastName; //store the last name
private String middleName;
//Default constructor
//Initialize firstName and lastName to an empty string.
//Postcondition: firstName = ""; lastName = "";
public Person()
{
firstName = "";
lastName = "";
middleName = "";
}
//Constructor with parameters
//Set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last;
public Person(String first, String middle,String last)
{
setName(first,middle, last);
}
//Method to output the first name and last name
//in the form firstName lastName.
public String toString()
{
return (firstName + " " + middleName + " " +lastName);
}
//Method to set firstName and lastName according to
//the parameters.
//Postcondition: firstName = first; lastName = last;
public void setName(String first,String middle, String last)
{
firstName = first;
middleName = middle;
lastName = last;
}
//Method to return firstName.
//Postcondition: The value of firstName is returned.
public String getFirstName()
{
return firstName;
}
//Method to return lastName.
//Postcondition: The value of lastName is returned.
public String getLastName()
{
return lastName;
}
public String getMiddleName()
{
return middleName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setMiddleName(String middleName)
{
this.middleName = middleName;
}
public boolean equalsLastName(Person p)
{
if(this.lastName == p.lastName)
return true;
else
return false;
}
public boolean equalsMiddleName(Person p)
{
if(this.middleName == p.middleName)
return true;
else
return false;
}
public boolean equalsFirstName(Person p)
{
if(this.firstName == p.firstName)
return true;
else
return false;
}
public boolean equals(Person p)
{
if(this.lastName == p.lastName && this.firstName == p.firstName)
return true;
else
return false;
}
public void makeCopy(Person p)
{
this.firstName = p.firstName;
this.middleName = p.middleName;
this.lastName = p.lastName;
}
public Person getCopy(Person p)
{
Person p1 = new Person();
p1.firstName = p.firstName;
p1.middleName = p.middleName;
p1.lastName = p.lastName;
return p1;
}
}
class TestPerson
{
public static void main (String[] args)
{
Person p1 = new Person("Tim","J.","Hawkins");
Person p2 = new Person();
p2.makeCopy(p1);
if(p1.equals(p2) == true)
System.out.println("Both persons are same .");
else
System.out.println("Persons are different .");
System.out.println(p2);
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.