Suppose that you and I are friends on Facebook. and you want to figure out the f
ID: 3926187 • Letter: S
Question
Suppose that you and I are friends on Facebook. and you want to figure out the friends we have in common ne 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 isn'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 program should include these three methods: This method should allow' the user to enter in a list of ID numbers, store those numbers into an array, and return file array that was created. Before allowing the user to enter the list, the method should ask the user to enter the list, the method should ask the user to enter how large the array should be. 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. For example, if the first array contained the ID numbers {5.4, 6, 7.2} and the second array contained 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. The main method should call readFriendlDs twice to allow the user to enter in two lists of friend IDs. Then it should call displayCommonFriedIDs on those two lists to show the intersection.Explanation / Answer
import java.util.*;
public class CommonFriend
{
public static void main(String ss[])
{
//Two friend ID array
int fID1[], fID2[];
System.out.println(" Enter First Friend ID array length: ");
fID1 = readFriendIDs();
System.out.println(" Enter Second Friend ID array length: ");
fID2 = readFriendIDs();
//Displays the contents of the first friend ID list
System.out.println(" First Friend IDs : ");
for(int c = 0; c < fID1.length; c++)
System.out.print(fID1[c] + " ");
//Displays the contents of the second friend ID list
System.out.println(" Second Friend IDs : ");
for(int c = 0; c < fID2.length; c++)
System.out.print(fID2[c] + " ");
//Find out the common friend
displayCommonFriendIDs(fID1, fID2);
}
//Returns the ID list after accepting data
public static int[] readFriendIDs()
{
int no1, t, f = 0;
//Scanner calss object created to accept data
Scanner sc = new Scanner(System.in);
//Accepts the length of the array
no1 = sc.nextInt();
//Creates a temporaty array of specified length
int temp [] = new int[no1];
System.out.println(" Enter Friend ID");
for(int c = 0; c < no1; )
{
t = sc.nextInt();
//Restricts duplicate ID
for(int d = 0; d < c; d++)
{
if(t == temp[d])
{
f = 1; //Sets the flag to 1 if data is already available
System.out.println(" Error: Duplicate ID not allowed: ");
break;
}
}
//If ID is not available in the list f value is not 1
//So store the valid ID
if(f == 0)
{
temp[c] = t;
c++;
}
//Reset the flag to 0
f = 0;
}
return temp;
}
//Checks and displays the common ID from two array
public static void displayCommonFriendIDs(int a[], int b[])
{
int f, s, c = 0, len;
//Calculates the length of both the array
int flen = a.length;
int slen = b.length;
if(flen > slen)
len = slen;
else
len = flen;
//Sets the common array length equal to the biggest length out of the two array
int comm[] = new int[len];
for(f = 0; f < flen; f++)
{
for(s = 0; s < slen; s++)
{
//If both array having common ID store it in common array
if(a[f] == b[s])
{
comm[c] = a[f];
c++;
}
}
}
//Displays the common array ID
System.out.println(" There are " + c + " friends in common: IDs ");
for(f = 0; f < c; f++)
if(f < c - 1)
System.out.print(comm[f] + ", ");
else
System.out.print(comm[f] + ".");
}
}
Output 1:
Enter First Friend ID array length:
5
Enter Friend ID
1
2
3
1
Error: Duplicate ID not allowed:
4
3
Error: Duplicate ID not allowed:
5
Enter Second Friend ID array length:
8
Enter Friend ID
11
22
33
1
5
11
Error: Duplicate ID not allowed:
44
55
66
First Friend IDs :
1 2 3 4 5
Second Friend IDs :
11 22 33 1 5 44 55 66
There are 2 friends in common: IDs
1, 5.
Output 2:
Enter First Friend ID array length:
10
Enter Friend ID
12
2
3
4
5
6
7
7
Error: Duplicate ID not allowed:
8
9
10
Enter Second Friend ID array length:
11
Enter Friend ID
22
33
12
44
2
3
6
66
10
7
5
First Friend IDs :
12 2 3 4 5 6 7 8 9 10
Second Friend IDs :
22 33 12 44 2 3 6 66 10 7 5
There are 7 friends in common: IDs
12, 2, 3, 5, 6, 7, 10.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.