// Instances of this class represent a person. // // Complete the methods of thi
ID: 666772 • Letter: #
Question
// Instances of this class represent a person. // // Complete the methods of this class so that it passes the tests. public class Person { final String name; final int age; public Person (String name, int age) { this.name = name; this.age = age; } public String toString () { return "Person("" + name + "", " + age + ")"; } // EXERCISE 1: Provide an implementation of the "hashCode" method //following the criteria laid out in the textbook and slides. // In "hashCode" and "equals" below, you should assume that people are equal if and only if //they have the same name and age, //i.e., there cannot be more than one person with name "alice" and age 50. public int hashCode () { // TODO: Complete this method. return 17; } // EXERCISE 2: Provide an implementation of the "equals" method following the criteria laid //out in the textbook and slides. public boolean equals (Object o) { // TODO: Complete this method. return false; } }
Explanation / Answer
implementation
This will be the below function implementation.
public boolean equals(Object o) {
if (o instanceof Person) {
return ((Person)o.age.equals(this.age);
}
}
@Override
public int hashCode() {
return this.age.hashCode();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.