Problem : A file named Friends.txt (below) is shown in Figure 1. Each row has a person's
Question:
Problem: A file named Friends.txt (below) is shown in Figure 1. Each row has a person's name, followed by a colon, and then several comma-separated-values (CSVs) that are the person's friends' names. So, Alice has five friends: Bob, Joe, Sara, Frank, and Nancy. A program that produces the outputs for 4 menu options as shown in Fig 2 to 5.
Alice: Bob, Joe, Sara, Frank, Nancy Bob: Alice, Nancy, Peter, Steve, Frank, Tim, Amy Charles: Sara, Kevin, Peter, Steve David: Steve, Amy Frank: Alice, Bob, Mary Fig1: Friends.txt |
*** Welcome to Friendly *** 1. Find the number of friends a person has 2. Find the number of common friends between two persons 3. Find the names of common friends between two persons 4. Exit 1 Enter the person's name alice alice has 5 friends 1. Bob 2. Joe 3. Sara 4. Frank 5. Nancy ----------------------------- Fig2: Menu option 1 Note: The menu options will be displayed again below the last dotted line shown above. The program will exit only when the user chooses option 4. | *** Welcome to Friendly! *** 1. Find the number of friends a person has 2. Find the number of common friends between two persons 3. Find the names of common friends between two persons 4. Exit 2 Enter first person's name alice Enter second person's name bob alice and bob have 2 common friends ----------------------------- Fig. 3: Menu option 2 |
*** Welcome to Friendly! *** 1. Find the number of friends a person has 2. Find the number of common friends between two persons 3. Find the names of common friends between two persons 4. Exit 3 Enter first person's name alice Enter second person's name bob alice and bob have 2 common friends 1. Frank 2. Nancy ----------------------------- Fig. 4: Menu option 3 | *** Welcome to Friendly! *** 1. Find the number of friends a person has 2. Find the number of common friends between two persons 3. Find the names of common friends between two persons 4. Exit 3 Enter first person's name david Enter second person's name frank david and frank have 0 common friends ----------------------------- Fig.5: Menu option 3. |
Solution design: The program has Friendly.java that has six methods as shown in the class diagram below.
The main()and getInputOutput() methods are fully coded. You need to code rest of the four methods.
- readFriends(): reads Friends.txt and populates persons[] and personFriends[]. For this purpose:
- Read data from Friends.txt into two arrays: persons and personFriends,
- The persons array should have names of persons (names before the colon in each row)
- The personFriends array should have names of the friends of the person at the same index as in persons array. E.g. persons[0] is Alice, and so personFriends[0] is an array of Alice's friends. Notice that each person has a different number of friends.
- findFriends(): given a name, returns an array of names of friends the person has.
- countCommonFriends(): given two names, returns the number of their common friends
- findCommonFriends(): given two names, returns an array with the names of their common friends.
Note:
- All input is case-insensitive.
- You don't need to handle garbage-input or names not existing in Friends.txt
Constraint: Use of Collection classes is not allowed. You must use arrays only. !!!!!!!!!!!!!!!!!!!!!!!
Instructions:
- Create a package named lab2 and copy/import java files in it
- Copy/import Friends.txt in the project folder
- Complete code as needed
Friendly.java :
package lab2;
import java.util.Scanner;
public class Friendly {
String[] persons;
String[][] personFriends;
public static void main(String[] args) {
Friendly friendly = new Friendly();
friendly.readFriends("friends.txt");
friendly.getInputOutput();
}
//do not change this method
void getInputOutput() {
int choice = 0;
Scanner input = new Scanner(System.in);
do {
System.out.println("*** Welcome to Friendly! ***");
System.out.println("1. Find the number of friends a person has");
System.out.println("2. Find the number of common friends between two persons");
System.out.println("3. Find the names of common friends between two persons");
System.out.println("4. Exit");
choice = input.nextInt();
input.nextLine(); //clear the buffer
switch (choice) {
case 1: {
System.out.println("Enter the person's name");
String name = input.nextLine();
String[] friends = findFriends(name);
if (friends != null) {
System.out.printf("%s has %d friends%n", name, friends.length);
int count = 0;
for (String s : friends ) {
System.out.println(++count + ". " + s );
}
} else System.out.println("Sorry! No friends found!");
System.out.println("-----------------------------");
break;
}
case 2: {
System.out.println("Enter first person's name");
String name1 = input.nextLine();
System.out.println("Enter second person's name");
String name2 = input.nextLine();
System.out.printf("%s and %s have %d common friends%n", name1, name2, countCommonFriends(name1, name2));
System.out.println("-----------------------------");
break;
}
case 3: {
System.out.println("Enter first person's name");
String name1 = input.nextLine();
System.out.println("Enter second person's name");
String name2 = input.nextLine();
String[] commonFriends = findCommonFriends(name1, name2);
if (commonFriends != null) {
System.out.printf("%s and %s have %d common friends%n", name1, name2, commonFriends.length);
int count = 0;
for (String s : commonFriends) {
System.out.println(++count + ". " + s);
}
} else System.out.println("Sorry! No match found!");
System.out.println("-----------------------------");
break;
}
default: System.out.println("Goodbye!");break;
}
} while (choice != 4);
input.close();
}
/** readFriends() reads the file with filename to
* populate persons and personFriends arrays
*/
void readFriends(String filename) {
// code here
}
/** given a name, returns an array of friends a person has
* If the name is not found, it returns null
*/
String[] findFriends(String name) {
// code here
return null;
}
/** given two names, returns how many common friends they have */
int countCommonFriends(String name1, String name2) {
//code here
return 0;
}
/**given two names, returns an array of names of common friends.
* If there are no common friends, then it returns an empty array, i.e. array of size 0*/
String[] findCommonFriends(String name1, String name2) {
// code here
return null;
}
}
Friends.txt :
Alice: Bob, Joe, Sara, Frank, Nancy Bob: Alice, Nancy, Peter, Steve, Frank, Tim, Amy Charles: Sara, Kevin, Peter, Steve David: Steve, Amy Frank: Alice, Bob, Mary