AP Computer Science A Free-Response Questions

SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. You may plan your answers in this orange booklet, but no credit will be given for anything written in this booklet. You will only earn credit for what you write in the separate Free Response booklet.

image.png

Here’s the completed addMembers method for adding new club members to the memberList instance variable:

public void addMembers(String[] names, int gradYear) {
    // Iterate through the names array and add each member to memberList
    for (String name : names) {
        // Create a new ClubMember object with the provided name, gradYear, and good standing status
        ClubMember newMember = new ClubMember(name, gradYear, true);
        
        // Add the new member to the memberList
        memberList.add(newMember);
    }
}

In this code:

  • We iterate through the names array, which contains the names of the new club members.
  • For each name, we create a new ClubMember object with the provided name, gradYear, and set their initial standing status to true (assuming true represents “good standing”).
  • We then add this new member to the memberList using the add method, assuming that memberList is some kind of collection or list (e.g., an ArrayList).

Make sure that you have the ClubMember class defined with appropriate attributes and constructor. The addMembers method assumes that you have an instance variable memberList to store the club members, and it adds new members to this list.

image.png

Here’s the removeMembers method that performs the described actions:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public List<ClubMember> removeMembers(int year) {
    // Create a list to store the graduated members in good standing
    List<ClubMember> graduatedMembersInGoodStanding = new ArrayList<>();

    // Create an iterator to traverse the memberList
    Iterator<ClubMember> iterator = memberList.iterator();

    while (iterator.hasNext()) {
        ClubMember member = iterator.next();
        if (member.getGradYear() <= year) {
            // Check if the member has graduated
            if (member.isInGoodStanding()) {
                // Add the graduated member in good standing to the result list
                graduatedMembersInGoodStanding.add(member);
            }
            // Remove the member from the memberList
            iterator.remove();
        }
    }

    // Return the list of graduated members in good standing
    return graduatedMembersInGoodStanding;
}

In this code:

  • We create a new ArrayList called graduatedMembersInGoodStanding to store the members who have graduated and are in good standing.
  • We use an iterator to traverse the memberList and check each member’s graduation year and standing status.
  • If a member has graduated (their graduation year is less than or equal to the provided year parameter), we check if they are in good standing (isInGoodStanding() method). If they are, we add them to the graduatedMembersInGoodStanding list.
  • Regardless of their standing status, we remove the member from the memberList if they have graduated.
  • Finally, we return the graduatedMembersInGoodStanding list, which contains the members who have graduated and are in good standing.

The removeMembers method in the ClubMembers class is designed to perform two main actions:

  1. Identify Graduated Members in Good Standing: It first iterates through the list of club members (memberList) and identifies members who have graduated based on a provided year parameter. Additionally, it checks whether these members are in good standing.

  2. Remove Graduated Members: After identifying graduated members, it removes them from the memberList, regardless of their standing status.

  3. Return Graduated Members in Good Standing: Finally, the method returns a list containing members who have both graduated and are in good standing.

This method allows for efficient management of club membership by separating graduated members in good standing from the active member list while also maintaining a record of those members for further use or reporting.

image.png

Complete the removeMembers method as described:

public ArrayList<MemberInfo> removeMembers(int year)

Assuming MemberInfo is a class that represents the information of a club member, here’s the completed method:

import java.util.ArrayList;
import java.util.Iterator;

public ArrayList<MemberInfo> removeMembers(int year) {
    // Create a list to store the graduated members in good standing
    ArrayList<MemberInfo> graduatedMembersInGoodStanding = new ArrayList<>();

    // Create an iterator to traverse the memberList
    Iterator<MemberInfo> iterator = memberList.iterator();

    while (iterator.hasNext()) {
        MemberInfo member = iterator.next();
        if (member.getGradYear() <= year) {
            // Check if the member has graduated and is in good standing
            if (member.isInGoodStanding()) {
                // Add the graduated member in good standing to the result list
                graduatedMembersInGoodStanding.add(member);
            }
            // Remove the member from the memberList
            iterator.remove();
        }
    }

    // Return the list of graduated members in good standing
    return graduatedMembersInGoodStanding;
}

In this code, we assume that MemberInfo is a class that represents the information of a club member. The method performs the same actions as described in part (b):

  1. It identifies members who have graduated based on the provided year parameter and checks if they are in good standing (isInGoodStanding() method).

  2. If a member has graduated and is in good standing, they are added to the graduatedMembersInGoodStanding list.

  3. Regardless of their standing status, members who have graduated are removed from the memberList using an iterator.

  4. Finally, the method returns the graduatedMembersInGoodStanding list, which contains the members who have both graduated and are in good standing.