Many of the AP Computer Science Free Response Problems either explicitly test your ability to work with arrays and lists or implicitly require such ability. This problem is intended as practice with commonly tested techniques.

Please don’t use this problem to practice your timing for the AP CS A Free Response section. Use only actual released AP CS FR for timing practice. The approach for this problem is identical to a real AP CS FR.

Problem description

An organization interviews candidates for a variety of positions.

The Candidate class stores a candidate’s ID number, the position for which the candidate is applying, and the score the candidate received during the candidate’s interview. The declaration of the Candidate class is shown below.

public class Candidate
{
    /** Constructs a Candidate object. */
    public Candidate(int idNumber, String position, int interviewScore)
    { /* implementation not shown */ }

    /** @return the position for which the candidate is applying */
    public String getPosition()
    { /* implementation not shown */ }

    /** @return the candidate's interview score */
    public int getInterviewScore()
    { /* implementation not shown */ }

    // There may be instance variables, constructors, and methods that are not shown.
}

The CandidatePool class maintains a list of the candidates interviewed. The declaration of the CandidatePool class is shown below. You will write methods of the CandidatePool class.

public class CandidatePool
{
    // The list of all candidates
    private ArrayList<Candidate> pool;

    /** Constructs a CandidatePool object. */
    public CandidatePool()
    {
        pool = new ArrayList<Candidate>();
    }

    /**
     * Adds candidate to the pool.
     * @param candidate the candidate to add to the pool
     */
    public void addCandidate(Candidate candidate)
    {
        pool.add(candidate);
    }

    /**
     * Returns a list of candidates from the pool that applied for the specified position.
     * @param position the position of candidates to return
     * @return a list of candidates that have the desired position
     */
    public ArrayList<Candidate> getCandidatesForPosition(String position)
    {
        /* to be implemented in part (a) */
    }

    /**
     * Returns the candidate from the pool with the highest interview score that
     * applied for the specified position. Returns null if there are no candidates
     * in the pool that applied for the specified position.
     * @param position the position of the candidate to return
     * @return the candidate with the highest interview score that applied for position or null
     */
    public Candidate getBestCandidate(String position)
    {
        /* to be implemented in part (b) */
    }

    /**
     * Removes all candidates from the pool that applied for the specified position.
     * @param position the position of candidates to remove from the pool
     * @return the number of candidates removed from the pool
     */
    public int removeCandidatesForPosition(String position)
    {
        /* to be implemented in part (c) */
    }

    // There may be instance variables, constructors, and methods that are not shown.
}

The example below shows candidates applying for the positions "Captain" and "First Mate".

CandidatePool shipPool = new CandidatePool();

shipPool.addCandidate(new Candidate(1, "Captain", 15));
shipPool.addCandidate(new Candidate(2, "Captain", 13));
shipPool.addCandidate(new Candidate(3, "First Mate", 20));
shipPool.addCandidate(new Candidate(4, "Captain", 10));
shipPool.addCandidate(new Candidate(5, "First Mate", 22));

Part (a) getCandidatesForPosition method

The getCandidatesForPosition method returns the list of candidates that applied for the specified position. If no candidates applied for the specified position the method returns an empty list.

For the shipPool example shown above, the call
shipPool.getCandidatesForPosition("Captain")
would return a list containing the candidates with ID numbers 1, 2, and 4.

For the shipPool example shown above, the call
shipPool.getCandidatesForPosition("First Mate")
would return a list containing the candidates with ID numbers 3 and 5.

For the shipPool example shown above, the call
shipPool.getCandidatesForPosition("Deck hand")
would return an empty list.

Complete method getCandidatesForPosition.

/**
 * Returns a list of candidates from the pool that applied for the specified position.
 * @param position the position of candidates to return
 * @return a list of candidates that have the desired position
 */
public ArrayList<Candidate> getCandidatesForPosition(String position)

Part (b) getBestCandidate method

The getBestCandidate method returns the candidate with the highest interview score that applied for the specified position. If no candidates applied for the specified position the method returns null.

For the shipPool example shown above, the call
shipPool.getBestCandidate("Captain")
would return the candidate with ID number 1.

For the shipPool example shown above, the call
shipPool.getBestCandidate("First Mate")
would return the candidate with ID number 5.

For the shipPool example shown above, the call
shipPool.getBestCandidate("Deck hand")
would return null.

Assume that getCandidatesForPosition works as specified, regardless of what you wrote in Part (a).

Complete method getBestCandidate.

/**
 * Returns the candidate from the pool with the highest interview score that
 * applied for the specified position. Returns null if there are no candidates
 * in the pool that applied for the specified position.
 * @param position the position of the candidate to return
 * @return the candidate with the highest interview score that applied for position or null
 */
public Candidate getBestCandidate(String position)

Part (c) removeCandidatesForPosition method

The method removeCandidatesForPosition updates the pool by removing all candidates that applied for the specified position. The pool may contain zero or more candidates that applied for the specified position. The method returns the number of candidates removed from the pool.

For the shipPool example shown above, the call
shipPool.removeCandidatesForPosition("Captain")
would return 3. After the call, the pool would contain the candidates with ID numbers 3 and 5.

For the shipPool example shown above, the call
shipPool.removeCandidatesForPosition("Deck hand")
would return 0. The pool would not be changed.

Complete method removeCandidatesForPosition.

/**
 * Removes all candidates from the pool that applied for the specified position.
 * @param position the position of candidates to remove from the pool
 * @return the number of candidates removed from the pool
 */
public int removeCandidatesForPosition(String position)

Solution & comments

See the CandidatePool solution or review it with AP CS Tutor Brandon Horn.

Comment on CandidatePool free response