In: Computer Science
The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print the name of a person. Redefine the class Person to include the following operations: 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 contain the same first and last names. Add the method makeCopy that copies the instance of variables of one Person object into another Person object. Add the method getCopy that creates and returns the address of the object, it is a copy of another Person Object. Write the definitions of the methods for the class Person to implement the operations. Write a program to test various operations of the class Person. Hint: You can use the class type Person inside your Person class definition.
public class Person { private String firstName; //store the first name private String lastName; //store the last name //Default constructor //Initialize firstName and lastName to an empty string. //Postcondition: firstName = ""; lastName = ""; public Person() { firstName = ""; lastName = ""; } //Constructor with parameters //Set firstName and lastName according to the parameters. //Postcondition: firstName = first; lastName = last; public Person(String first, String last) { setName(first, last); } //Method to output the first name and last name //in the form firstName lastName. public String toString() { return (firstName + " " + lastName); } //Method to set firstName and lastName according to //the parameters. //Postcondition: firstName = first; lastName = last; public void setName(String first, String last) { firstName = first; 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; } }
//The person class
public class Person { private String firstName; private String middleName; private String lastName; //Default constructor public Person() { firstName = ""; lastName = ""; middleName =""; } //Constructor with parameters public Person(String first,String middle, String last) { setName(first,middle,last); } // Method to set the last name: //Method to set the last name of a person public void setLastName(String last) { this.lastName=last; } // Method to set the first name: //Method to set the first name of a person public void setFirstName(String first) { this.firstName=first; } // Method to set the middle name: //Method to set the middle name of a person public void setMiddleName(String middle) { this.middleName=middle; } //Method definition of copy constructor that takes //person object variable and copy the instance values //to the class person object //Copy Constructor public Person(Person obj) { firstName=obj.firstName; middleName=obj.middleName; lastName=obj.lastName; } // method that sets the name public void setName(String first,String middle,String last){ firstName=first; middleName=middle; lastName=last; } // method sets the last name // method checks for previous name if it is equal public void setLast(String last){ if(last.equals(getLastName())) System.out.println("Equal Previous name"); else System.out.println("Not Equal"); lastName=last; } // method sets the first name // method checks for previous name if it is equal public void setFirst(String first){ if(first.equals(getFirstName())) System.out.println("Equal Previous name"); else System.out.println("Not Equal"); firstName=first; } // method sets the middle name // method checks for previous name if it is equal public void setMiddle(String middle){ if(middle.equals(getMiddleName())){ System.out.println("Equal Previous name");} else{ System.out.println("Not Equal"); middleName=middle; } } // returns the first name public String getFirstName() { return firstName; } // returns the middle name public String getMiddleName() { return middleName; } public String getLastName(){ return lastName; } //The method equals that takes person object as // its argument that returns true if two objects // contain the same first ,last and middle name // checks for the objects having same names public boolean equals(Person obj) { return(firstName==obj.firstName && middleName==obj.middleName&& lastName==obj.lastName); } //Method makeCopy that takes person object as //variables into the another person class //copies one object name to another public void makeCopy(Person obj) { firstName=obj.firstName; middleName=obj.middleName; lastName=obj.lastName; } public Person getCopy() { Person obj=new Person(); obj.firstName=firstName; return obj; } // prints the object details public String toString() { return (firstName +" "+middleName+ " " + lastName); } }
//The Test Class
public class Test { public static void main(String[] args) { Person person1 = new Person(); Person person2 = new Person("Tom","Dick","Harry"); Person person3 =new Person("Ashley","Robert", "Junior"); Person person = new Person(person2); System.out.println("String Person :"+person); System.out.println("String Person2 :"+person2); System.out.println("String Person3 :"+person3); System.out.println("The objects are equal: "+person1.equals(person2)); person1=person2.getCopy(); System.out.println("After get copy person1:"+person1); person2.makeCopy(person3); System.out.println("After make Copy P2 :"+person2); } }
//output image