AP Computer Science A Free Response Practice - 2D Array - Mutual Connections
Mutual Connections
This single part question is worth 6 points.
AP CSA Alignment
| Unit Alignment | Learning Objectives | Resources | Skills |
|---|---|---|---|
| Unit 1: Using Objects and Methods | 1.10.A Develop code to call class methods and determine the result of those calls. | 2.C Write program code involving procedural abstractions. | |
| Unit 2: Selection and Iteration | 2.3.A Develop code to represent branching logical processes by using selection statements and determine the result of these processes. | 2.A Write program code to implement an algorithm. | |
| Unit 4: Data Collections | 4.12.A Develop code used to traverse the elements in a 2D array and determine the result of these traversals | 2.B Write program code involving data abstractions. | |
| 4.13.A Develop code for standard and original algorithms for a particular context or specification that involves 2D arrays and determine the result of these algorithms. |
Directions
The Relationship class is used to store information about a person's social life and connections. A partial declaration of the Relationship class is shown.
The SocialNetwork class models a social network using a 2D array of Relationship objects. A partial declaration of the SocialNetwork class is shown.
Write the countMutualFriends method. The method should return the number of users who are mutual friends with the given user. Two users are mutual friends if BOTH users are consider the other a friend. It is possible for a user to consider another user as a friend and that not be reciprocated.
Each row of connections represents a user. There are 0 to the number of rows - 1 users. Each column also represents a user. So the user at row 0 is the same as the user at column 0.
Suppose there are 4 users (numbered 0 to 3). Consider following results when calling isFriend:
- Example 1: calling
isFriendon the object at row 0, column 1 results intrueand callingisFriendon the object at row 1, column 0 results intrue. Therefore, user 0 and user 1 are mutual friends. - Example 2: calling
isFriendon the object at row 0, column 2 results intrueand callingisFriendon the object at row 2, column 0 results infalse. Therefore, user 0 and user 2 are not mutual friends. - Example 3: calling
isFriendon the object at row 0, column 3 results infalseand callingisFriendon the object at row 3, column 0 results infalse. Therefore, user 0 and user 2 are not mutual friends.
As a result call countMutualFriends(0) would return 1.
Note: that the user is never compared with itself.
Write Your Response
- In order to test your program, we have provided some test cases for calls to
countMutualFriends. - Write the code for the method
countMutualFriendsin the Java Playground as indicated by the commentadd your code here. - All tests should return
trueif your code is correct.