Assume that a Person has data fields lastName and firstName. Write an equals met
ID: 3821900 • Letter: A
Question
Assume that a Person has data fields lastName and firstName. Write an equals method that returns true if two Person objects have the same first and last names. Write a hashCode method that satisfies the hashCode contract. Make sure that your hashCode method does not return the same value for Henry James and James Henry. Your equals method should return a value of false for these two people.(equals method and hashCode method) Assume that a Person has data fields lastName and firstName. Write an equals method that returns true if two Person objects have the same first and last names. Write a hashCode method that satisfies the hashCode contract. Make sure that your hashCode method does not return the same value for Henry James and James Henry. Your equals method should return a value of false for these two people.(equals method and hashCode method)Explanation / Answer
Person.java
public class Person {
// Declaring instance variables
private String firstname;
private String lastname;
// Parameterized constructor
public Person(String firstname, String lastname) {
super();
this.firstname = firstname;
this.lastname = lastname;
}
// getters and setters
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result
+ ((lastname == null) ? 0 : lastname.hashCode());
return result;
}
public boolean equals(Person other) {
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
return true;
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Person [firstname=" + firstname + ", lastname=" + lastname+ "]";
}
}
_________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
String fname,lname;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
System.out.println("__Person#1__");
//getting the inputs entered by the user
System.out.print("Enter firstname :");
fname=sc.next();
System.out.print("Enter lastname :");
lname=sc.next();
//Creating a Person#1 class Object
Person p1=new Person(fname, lname);
System.out.println("__Person#2__");
//getting the inputs entered by the user
System.out.print("Enter firstname :");
fname=sc.next();
System.out.print("Enter lastname :");
lname=sc.next();
//Creating a Person#2 class Object
Person p2=new Person(fname, lname);
//Checking whether the two Person are Equal or not
boolean bool=p1.equals(p2);
if(bool)
System.out.println("Person#1 is equal to Person#2");
else
System.out.println("Person#1 is not equal to Person#2");
//Getting hashcode of Person#1 and Person#1
int hcode1=p1.hashCode();
int hcode2=p2.hashCode();
//Comparing hashcodes of Person#1 and Person#2
if(hcode1==hcode2)
System.out.println("Hascode of Person#1 is equal to Person#2");
else
System.out.println("Hashcode of Person#1 is not equal to Person#2");
}
}
____________________
Output:
__Person#1__
Enter firstname :Williams
Enter lastname :Kane
__Person#2__
Enter firstname :Kane
Enter lastname :Williams
Person#1 is not equal to Person#2
Hashcode of Person#1 is not equal to Person#2
______________
Output#2:
__Person#1__
Enter firstname :Kane
Enter lastname :Williams
__Person#2__
Enter firstname :Kane
Enter lastname :Williams
Person#1 is equal to Person#2
Hascode of Person#1 is equal to Person#2
__________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.