import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
Person myself = new Person("Ram",
"Prasad", "Bismil");
myself.setFirstName("Shyam");
myself.setMiddleName("Prasad"); ///
testing by passing same middle name;
myself.setLastName("Sunder");
Person you = myself.getCopy(); //// test for makeCopy() as well as getCopy()
System.out.println(myself.equals(you));
}
}
class Person
{
String firstName;
String middleName;
String lastName;
///// default constructor ////
public Person()
{
}
//// parameterized constructor //////
public Person(String firstName, String middleName,
String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
}
///// getter for full name /////
public String getName()
{
return firstName + " " + middleName
+ " " + lastName;
}
//// setter for first name /////
public void setFirstName(String firstName)
{
////// if given first name is same
as already stored first name, this block will execute
if(firstName.equals(this.firstName))
{
System.out.println("given first name is same as the already stored
firstName");
return;
}
this.firstName = firstName;
}
//// setter for middle name /////
public void setMiddleName(String middleName)
{
////// if given middle name is same
as already stored middle name, this block will execute
if(middleName.equals(this.middleName))
{
System.out.println("given middleName name is same as the already
stored middleName");
return;
}
this.middleName = middleName;
}
//// setter for last name /////
public void setLastName(String lastName)
{
////// if given last name is same
as already stored last name, this block will execute
if(lastName.equals(this.lastName))
{
System.out.println("given last name is same as the already stored
last name");
return;
}
this.lastName = lastName;
}
/////// equals method to comapre two Person objects
/////////
public boolean equals(Person obj)
{
if(this.firstName.equals(obj.firstName) &&
this.lastName.equals(obj.lastName))
return
true;
else
return
false;
}
////// makeCopy function to create a copy of
current object
public void makeCopy(Person obj)
{
obj.firstName =
this.firstName;
obj.middleName =
this.middleName;
obj.lastName = this.lastName;
}
////// method to create a copy of current object and
return to the caller
public Person getCopy()
{
Person obj = new Person();
this.makeCopy(obj);
return obj;
}
}
Get Answers For Free
Most questions answered within 1 hours.