In java An overriden equals() method which identifies whether two people share t
ID: 3916080 • Letter: I
Question
In java An overriden equals() method which identifies whether two people share the same name, ignore the caseAn overriden compareto() method which would fulfill the sorting rule of a comparison sorting algorithm that shorts people in alphabetical ascending order by last name then first name, ignore the cas In java An overriden equals() method which identifies whether two people share the same name, ignore the case
An overriden compareto() method which would fulfill the sorting rule of a comparison sorting algorithm that shorts people in alphabetical ascending order by last name then first name, ignore the cas An overriden equals() method which identifies whether two people share the same name, ignore the case
An overriden compareto() method which would fulfill the sorting rule of a comparison sorting algorithm that shorts people in alphabetical ascending order by last name then first name, ignore the cas
Explanation / Answer
package com.example;
public class People
{
String firstName;
String lastName;
public People(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public boolean equals(People p)
{
if(this.firstName.toLowerCase().equals(p.firstName.toLowerCase()) && this.lastName.toLowerCase().equals(p.lastName.toLowerCase()))
return true;
else
return false;
}
public int compareTo(People p)
{
String lol1=this.lastName.toLowerCase();
String lol2=p.lastName.toLowerCase();
if(lol1.compareTo(lol2)==0)
{
String lol3=this.firstName.toLowerCase();
String lol4=p.firstName.toLowerCase();
return lol3.compareTo(lol4);
}
else
return lol1.compareTo(lol2);
}
public static void main(String[] args)
{
People p1=new People("Dileep","Bhagat");
People p2=new People("DILEEP","BHAGAT");
System.out.println(p1.equals(p2)); // output: true since both are same after ignoring the case & equals method() return true
System.out.println(p1.compareTo(p2)); // output:0 since both are same after ignoring the case
People p3=new People("Brajesh","Kumar");
System.out.println(p3.equals(p2)); // output: false since both are not same
System.out.println(p3.compareTo(p2)); // output: 9 since during comparison of Kumar & Bhagat k is 9 greater than b that's why it returns 9
People p4=new People("Anil","Kumar");
System.out.println(p4.equals(p3)); // output: false since both are not same
System.out.println(p4.compareTo(p3)); // output: -1 since during comparison of p4 & p3 both last name is equal but their first character of first name is different, and a is -1 greater than b that's why it returns -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.