Suppose that you and I are friends on Facebook, and you want to figure out the f
ID: 3539814 • Letter: S
Question
Suppose that you and I are friends on Facebook, and you want to figure out the friends we have in common. One way of doing this would be to treat your friends list as an array and my friends list as another array. Then, the problem of finding our common friends would be solvable by finding the intersection ( set of common elements ) between those two arrays. Assume that each friend is represented by a unique numerical ID number ( Using a person's name directly ins't a good idea, because it's possible for more than one person to have the same name.)
Write a program that accomplishes this task. Your prgram should include these three methods:
public static int[] readFriendIDs()
This method should allow the user to enter in a list of ID numbers, store those numbers into an array, and return the array that was created. Before allowing the user to enter the list, the method should ask the user to enter how large the array should be.
public static void displayCommonFriendIDs(int[] a, int[] b)
This method should take two arrays of friend IDs as parameters and display a list of all the IDs that appear in both a and b ( regardless of order ). The method should also display the count of common friends. Assume that both a and b contain distinct elements - that is , within each array there are no numbers repeated. However, this does NOT mean that a and b don't have any numbers in common. For example, if the first arary contained the ID numbers {5,4,6,7,2} and the second array the ID numbers { 6,5,2,10,4,11,15}, then calling this method should display that there are four friends in common: IDs 5,4,6, and 2. Hint: for each element of a, perform a linear search of b.
public static void main(String[] args)
The main method should call readFriendIDs twice to allow the user to enter in two lists of friend IDs. Then it should call displayFriendIDs on those two lists to show the intersection.
Explanation / Answer
import java.util.Scanner;
class CommonFriends
{
public static int[] readFriendIDs()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter number of friends (i.e size of array): ");
int n=s.nextInt();
int[] friends=new int[n];
System.out.println("Enter "+n+" ID's of friends:");
for(int i=0;i<n;i++)
friends[i]=s.nextInt();
return friends;
}
public static void displayCommonFriendIDs(int[] a, int[] b)
{
int count=0;
String ids="";
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if( a[i]==b[j])
{
count++;
ids = ids + ", "+a[i];
break;
}
}
}
System.out.println("There are "+count+" friends in common.");
System.out.println("ID's: "+ids.substring(2));
}
public static void main (String[] args)
{
System.out.println("Enter list 1:");
int[] fr1 = readFriendIDs();
System.out.println("Enter list 2:");
int[] fr2 = readFriendIDs();
displayCommonFriendIDs(fr1,fr2);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.