In: Computer Science
JAVA, this is practice to better understand java.
//Please note throughout if possible looking to better understand
the process.
Write the following class: Person.
Define Class Person
Write the class header
Class Variables
Class Person is to have the following data members: firstName of
type String, lastName of type String representing a person’s first
and last names and ssn of type int representing a social security
number. Each data member has an access specifier of type
private.
Constructors
Class Person is to have two constructors with the following
specifications:
1. a no-arg constructor that initializes each String to an empty
String and the integer data member to zero;
2. a constructor that takes three arguments, each representing one
of the class data members. The arguments are to be listed in the
order of (firstName, lastName, ssn);
Get and Set Methods
Class Person is to have standard get/set methods for each data
member.
Method Overrides
Class Person is to override the Object equals( ) method to do the
following:
1. test to see if the parameter represents an object;
2. test to see if the parameter object is of the same class type as
the calling object;
3. determine if the calling object and the parameter object store
identical data values for the corresponding data members.
Class Person is to override the Object toString( ) method to
display the following information in the format presented:
1. First Name: display firstName data
2. Last Name: display lastName data
3. SSN: display ssn data
Please find the class definition below:
class Person {
private String firstName;
private String lastName;
private int ssn;
public Person() {
this.firstName = "";
this.lastName = "";
this.ssn = 0;
}
public Person(String firstName, String lastName, int ssn) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSsn() {
return ssn;
}
public void setSsn(int ssn) {
this.ssn = ssn;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", ssn=" + ssn + "]";
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Person)) {
return false;
}
Person p = (Person) o;
// Compare the data members and return accordingly
if (p.getFirstName().equals(this.getFirstName()) && p.getLastName().equals(this.getLastName())
&& p.getSsn() == this.getSsn()) {
return true;
} else {
return false;
}
}
}