JAVA 4) Given: public class Employee Employee(String dob, String ssn, Address ad
ID: 2246795 • Letter: J
Question
JAVA
4) Given: public class Employee Employee(String dob, String ssn, Address address) this.dob = dob; this.ssn = ssn: this.address = address; private String dob; private String ssn; I/ date of birth Il social security number II Class that contains address information private Address address; publie boolean equals(Object object) t Two Employee objects are considered equal if their date of birth and social security numbers are the same. Implement the equals method above so that two Employee instances may be compared with the aforementioned criteria. if (empl.equals(emp2)) f System.out.println"empl and emp2 are the same!!" ; elset System.out.printine'empl and emp are different"):Explanation / Answer
Program Address.java
=======================================================
public class Address {
//Declare all required variables
private String street;
private String city;
//Default constructor
public Address() {
}
//Parameterized constructor
public Address(String street, String city) {
this.street = street;
this.city = city;
}
//All getters and setters method
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Street : "+street +" City : "+city;
}
}
Program Employee.java
=======================================================
public class Employee {
//Declare all required variables
private String dob;
private String ssn;
private Address address;
//Parameterized constructor
public Employee(String dob, String ssn, Address address) {
this.dob = dob;
this.ssn = ssn;
this.address = address;
}
//equals method to compare two employee objects
public boolean equals(Object object) {
//Type cast to employee object
Employee employeeOhter = (Employee)object;
//If dob and ssn of two employees are same return true
if(this.dob==employeeOhter.dob && this.ssn==employeeOhter.ssn) {
return true;
} else {
return false;
}
}
}
Program EmployeeTest.java
=======================================================
public class EmployeeTest {
public static void main(String[] args) {
//Create two employee objects
Employee emp1 = new Employee("15/10/1991", "5872-4209-2347", new Address("Houstan", "Texas"));
Employee emp2 = new Employee("15/10/1991", "5872-4209-2347", new Address("Houstan", "Texas"));
//Check for equality
if(emp1.equals(emp2)) {
System.out.println("Two employees are identical");
} else {
System.out.println("Two employees are not identical");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.