Question
JAVA Create a class named Test to test your classes. a. Create a generic method called printArrayList which takes a generic ArrayList as a parameter
JAVA
Create a class named Test to test your classes.
a. Create a generic method called printArrayList which takes a generic ArrayList as a parameter e.g. printArrayList(ArrayList al). This method should loop through the array list and print out each object.
b. Create an ArrayList to hold Person objects.
c. Create two Person objects and two Employee objects and store them in this ArrayList.
d. Pass this ArrayList to the printArrayList method.
e. Create two Employees whose PPS numbers are the same but whose names are slightly different (e.g. Al and Alan). Illustrate that these are not equal with regards to the == operator but are equal when the equals() method is used.
Here are Person and Employee classes:
public class Person { String name; int phoneNumber;
public String toString() { return "Person{" + "name='" + name + '\'' + ", phoneNumber=" + phoneNumber + '}'; }
public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (phoneNumber != person.phoneNumber) return false; return name != null ? name.equals(person.name) : person.name == null; }
}
public class Employee extends Person {
int PPS;
public String toString() {
return "Employee{" + "name=" + name + '\'' + ", phoneNumber="
+ phoneNumber + "PPS = " + PPS + '}';
}
public boolean equals(Object o) {
Employee person = (Employee) o;
if (this == o) {
return true;
}
if (!(o instanceof Person)) {
return false;
}
if (phoneNumber != person.phoneNumber) {
return false;
}
return name != null ? name.equals(person.name) : person.name == null;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started