Question: Using the class Person defined below create a class Friends that manages a list of Person. This class should have the following methods: add(Person p)

Using the class Person defined below create a class Friends that manages a list of Person. This class should have the following methods:

add(Person p) – add new person to set;

search(Person p) – returns true if p is a member of the set of friends, false otherwise;

size() – returns number of friends

getSurname(String s) – returns a set of just those persons whose surname is s;

getFirstname(String f) - returns a set of just those persons whose surname is f;

getFreq(String f) – returns number of persons whose first name is f;

del(Person p) – remove person p if present;

sort() – returns a sorted list of Person.

You may use a ArrayList or a LinkedList for your collection of Person.

final class Person implements Comparable<Person>{

         private final String sName;

  private final String fName;

         Person(String fn, String sn){fName = fn; sName = sn;}

         public String sName(){return sName;}

  public String fName(){return fName;}

         public String toString(){return fName+" "+sName;}

         public boolean equals(Object ob){

                  if(!(ob instanceof Person)) return false;

                  Person p = (Person)ob;

                  return sName.equals(p.sName) && fName.equals(p.fName);

         }

         public int compareTo(Person p){

                  if(p == null) return -1;

    if(this.equals(p)) return 0;

                  return sName.compareTo(p.sName);

         }

         public int hashCode(){

       return 41 * sName.hashCode() * fName.hashCode();

  }

}

Step by Step Solution

3.41 Rating (167 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Code import javautilArrayList import javautilScanner public class Friends static ArrayList al new ArrayList static Scanner sc static int choice 0 static String sname fname public static void mainStrin... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!