Skip Top Navigation Bar

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 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.
Unit 4: Data Collections 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. 2.B Write program code involving data abstractions.

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.

public class Relationship {
    /**
     * Returns true if this user considers this person a friend 
     * and false otherwise.
     */
    public boolean isFriend() {
        /* implementation not shown */
    }
}

The SocialNetwork class models a social network using a 2D array of Relationship objects. A partial declaration of the SocialNetwork class is shown.

public class SocialNetwork {
    /** 
     * precondition: connections is a square matrix (same 
     * number of rows and columns)
     * 
     * Each row represents a user. Each column also 
     * represents the same corresponding user.
     */
    private Relationship[][] connections;

    public SocialNetwork(Relationship[][] connect) {
        connections = connect;
    } 

    /**
     * Returns the number of users who are mutual friends 
     * with the given user.
     * Two users are mutual friends if BOTH users consider
     * the other as a friend. It is possible for a
     * user to consider another user to be a friend and 
     * that other user not reciprocate.  
     */ 

    public int countMutualFriends(int user) {
        /* to be implemented */
    }
}

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 isFriend on the object at row 0, column 1 results in true and calling isFriend on the object at row 1, column 0 results in true. Therefore, user 0 and user 1 are mutual friends.
  • Example 2: calling isFriend on the object at row 0, column 2 results in true and calling isFriend on the object at row 2, column 0 results in false. Therefore, user 0 and user 2 are not mutual friends.
  • Example 3: calling isFriend on the object at row 0, column 3 results in false and calling isFriend on the object at row 3, column 0 results in false. 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 countMutualFriends in the Java Playground as indicated by the comment add your code here.
  • All tests should return true if your code is correct.