ArrayList

public class MemberInfo
private name
private gradYear
private hasGoodStanding
public MemberInfo(String name, int gradYear, boolean hasGoodStanding)
public int getGradYear()
public boolean inGoodStanding()
}
public class ClubMembers
private ArrayList<MemberInfo> memberList; 
public ClubMembers(String[] names, int gradYear)
public void addMembers(String[] names, int gradYear)
public int count(int gradYear)
public ArrayList<MemberInfo> removeMembers(int year)

a) Write the ClubMembers constructor ClubMembers, which takes one parameters, which contains the names of new club members to be added. The method initialize the memberList by adding all new members. The names can be added in any order. All members added are initially in good standing and share the same graduation year, gradYear, which is 2026.

public ClubMembers(String[] names)

public ClubMembers(String[] names) {
    memberList = new ArrayList<MemberInfo>();
    for(int i=0; i<names.length; i++) {
        memberList.add(new MemberInfo(names[i], 2026, true));
    }
}

b) Write the ClubMembers method addMembers, which takes two parameters. The first parameter is a String array containing the names of new club members to be added. The second parameter is the graduation year of all the new club members. The method adds the new members to the memberList instance variable. The names can be added in any order. All members added are initially in good standing and share the same graduation year, gradYear.

Complete the addMembers method.

public void addMembers(String[] names, int gradYear)

public void addMembers(String[] names, int gradYear) {
    for(int i=0; i<names.length; i++) {
        memberList.add(new MemberInfo(names[i], gradYear, true));
    }
}

c) Write the ClubMembers method count, to return the number of students who gradated in a specific year.

public int count(int gradYear)

public int count(int gradYear)  {
    int result = 0;
    for(int i=0; i<memberList.size();i++) {
        if(memberList.get(i).getGradYear() == gradYear) {
            result++;
        }
    }
    return result;
}

d) Write the ClubMembers method removeMembers, which takes the following actions. Returns a list of all students who have graduated and are in good standing. A member has graduated if the member’s graduation year is less than or equal to the method’s year parameter. If no members meet these criteria, an empty list is returned.

Removes from memberList all members who have graduated, regardless of whether or not they are in good standing.

public ArrayList<MemberInfo> removeMembers(int year)

public ArrayList<MemberInfo> removeMembers(int year) {
    ArrayList<MemberInfo> result = new ArrayList<MemberInfo>();
    for(int i=memberList.size()-1; i>=0; i--) {
        MemberInfo m = memberList.get(i);
        if(m.getGradYear() <= year) {
            if(m.inGoodStanding()) {
                result.add(m);
            }
            memberList.remove(i);
        }
    }
    return result;
}

Last updated