Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the class Person, defined below, create a class Friends that manages a set of Person. Also use the test class given on the next

Using the class Person, defined below, create a class Friends that manages a set of Person. Also use the test class given on the next page to test your code.

The Friends 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 firstname is f;

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

del(Person p) remove person p if present; return true if removed, false otherwise;

sort() returns a sorted list (ArrayList or LinkedList) of Person.

display() prints out a list of all people in the format: [Joe Bloggs, Mary Murphy, Tim Robins]

You must use a TreeSet or a HashSet for your collection of Person.

The compareTo method needs to be updated

final class Person implements Comparable{

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();

}

}

Do not edit this code

public class FriendsTest {

public static void main(String[] args) {

Friends f = new Friends();

Person p1 = new Person("Joe", "Bloggs");

f.add(p1);

f.add(new Person("Rosella", "Murphy"));

f.add(new Person("Mary", "O Keefe"));

f.display();

System.out.println();

Person notThere = new Person("JJ", "Byrne");

System.out.printf("Person %s should not be there, so false: %b ",

notThere.toString(),f.search(notThere));

System.out.printf("Person %s should be there, so true: %b ",p1.toString(),f.search(p1));

System.out.println();

System.out.printf("There should be 3 friends: %d ",f.size());

System.out.println();

f.add(new Person("Rosella", "O Keefe"));

System.out.printf("There should be 2 people with the surname 'O Keefe: %s",

f.getSurname("O Keefe").toString());

System.out.println();

System.out.println();

f.add(new Person("Rosella", "O Keefe"));

System.out.printf("There should be 2 people with the firstname 'Rosella': %s ",

f.getFirstname("Rosella").toString());

System.out.println();

System.out.printf("There should be 1 person called 'Joe': %d ",f.getFreq("Joe"));

f.add(new Person("Joe", "Bloggs"));

System.out.printf("There should still be 1 person called 'Joe': %d ",f.getFreq("Joe")); // unique

f.add(new Person("Joe", "BloggsEile"));

System.out.printf("There should be 2 people called 'Joe': %d ",f.getFreq("Joe"));

System.out.println();

f.display();

System.out.printf("Jow Bloggs should be removed (4 people): ");

f.del(p1);

f.display();

System.out.println();

System.out.printf("Sorted list of friends: %s ",f.sort().toString());

}

}

Type or paste question here

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions

Question

When should a standard cost variance be investigated?

Answered: 1 week ago

Question

b. Will new members be welcomed?

Answered: 1 week ago

Question

a. What is the purpose of the team?

Answered: 1 week ago