Java Question Given a series of user actions in the social network that is initi
ID: 3737040 • Letter: J
Question
Java Question
Given a series of user actions in the social network that is initially empty, recommend new possible friendships after one user friends another user. For example, if Alice, Bob, and Carol are all network members, and Alice and Bob are friends. If Bob and Carol become friends, then your program should recommend that Alice and Carol should become friends. For example, given the user actions the resulting recommendations would be:
Alice joins
Carol joins
Bob joins
Bob friends Alice
Bob friends Carol
Dave joins
Dave friends Bob
end
Figure 2: Sample user actions.
Alice and Carol should be friends
Alice and Dave should be friends
Carol and Dave should be friends
Your task is to create a program that reads in the user actions and generates the friend recommendations.
Write a program called Friend.java that reads in the user actions from the console (System.in) and outputs the corresponding friend recommendations. Your Friend class must
implement the provided Tester interface and also contain the main() method where your program starts running. This is because your program will be tested via this interface. The interface contains a single method:
public ArrayList compute( Scanner input );
This method must perform the required computation.
Input
The compute() method takes a Scanner object, which contains one or more lines. Each line, except the last one, contains a user action. There are four user actions:
A joins : means that new user A has joins the network.
A friends B : means that user A and B are now friends.
A unfriends B : means that user A and B are no longer friends.
A leaves : means that user A has left the network (deleted their account).
where A and B are names of users. The user names are single words with no white spaces within them. The last line of input is simply the word end". See Figure 2 for an example. Hint: Use the nextLine() method of the Scanner object to read one line of input at a time, and then parse the line.
Semantics
The social network is initially empty. All user actions are on the same network. Only users that are not part of the social network can join. I.e., each user will have a unique name. Users can leave the network and then rejoin. When a user leaves the network, the user and all his/her friendships are deleted from the network. If a user rejoins the network, it's as if he/she is joining for the first time.
All friendships are symmetrical. I.e., "A friends B" is the same as "B friends A". No recommendation should be generated for users who are already friends.
Output
The method compute( Scanner input ) should return an ArrayList of Strings denoting the friend recommendations in the same order as the user actions that caused them. A
recommendation is of the form
A and B should be friends
where A and B are user names and must be in alphabetical order. For example, this recommendation is correct: "Alice and Bob should be friends", while this one is not: "Bob
and Alice should be friends". If more than one recommendation is generated by a single user action, the recommendations should be ordered alphabetically.
Hint
Use a 1-pass algorithm: Simply check for friendships after processing each user action.
The Program Must Pass All Tests in the Junit Test Class
Compute Method Tester Interface Class
Junit Test Class
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public interface Tester {
public ArrayList<String> compute( Scanner input );
}
-----------------------------------------------------
Friend.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Friend implements Tester {
private Map<String,ArrayList<String>> userConnections= new HashMap<String,ArrayList<String>>();
public ArrayList<String> compute(Scanner input) {
String line;
ArrayList<String> recommendations=new ArrayList<String>() ;
while((line=input.nextLine()).equalsIgnoreCase("end"))
{
if(line.contains("joins"))
{
String[] stringArray = line.split("");
userConnections.put(stringArray[0], new ArrayList<String>());
}
if(line.contains("friends"))
{
String[] stringArray = line.split("");
ArrayList<String> friends = userConnections.get(stringArray[0]);
friends.add(stringArray[2]);
userConnections.put(stringArray[0], friends);
}
}
for(Map.Entry<String,ArrayList<String>> entry:userConnections.entrySet())
{
String friend=entry.getKey();
List<String> friends=entry.getValue();
if(friends.size()>0)
{
if(friends.size()>1)
{
for(int i=0;i<friends.size();i++)
{
for(int j=1;j<friends.size();j++)
{
List<String> f=userConnections.get(friends.get(i));
if(!(f.contains(friends.get(j))))
{
char c1=friends.get(i).charAt(0);
char c2=friends.get(j).charAt(0);
if(c1 <= c2)
{
recommendations.add(friends.get(i)+" and " + friends.get(j) + "should be friends");
}
else
{
recommendations.add(friends.get(j)+" and " + friends.get(i) + "should be friends");
}
}
}
}
}
else
{
for(int i=0;i<friends.size();i++)
{
List<String> f=userConnections.get(friends.get(i));
for(String s:f)
{
if(!(friends.contains(s)))
{
char c1=friend.charAt(0);
char c2=s.charAt(0);
if(c1 <= c2)
{
recommendations.add(friend+" and " + s + "should be friends");
}
else
{
recommendations.add(s+" and " + friend + "should be friends");
}
}
}
}
}
}
}
return recommendations;
}
public static void Main(String args[])
{
Friend friend= new Friend();
ArrayList<String> recommendations= friend.compute(new Scanner(System.in));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.