CandidatePool free response practice question 1

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.

Problem description

An organization interviews candidates for a variety of positions. The Candidate class stores the position for which a candidate is applying and the score received during the candidate’s interview. The declaration of the Candidate class is shown below.

public class Candidate
{
 /** Constructs a new Candidate object */
 public Candidate(int idNumber, String position, double 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 double 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.

public class CandidatePool
{
 /** The list of all candidates */
 private List<Candidate> pool;
 
 /** Constructs a new 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 have the same position as position
  *  @param position the position of candidates to return
  *  @return a list of candidates that have the desired position */
 public List<Candidate> getCandidatesForPosition(String position)
 { /* to be implemented in part (a) */ }
 
 /** Returns the candidate from the pool with the highest interview score that
  *  has the same position as position or null if position does not match the
  *  position of any candidate.
  *  @param position the position of the candidate to return
  *  @return the candidate for position with the highest interview score or null */
 public Candidate getBestCandidate(String position)
 { /* to be implemented in part (b) */ }
 
 /**
  * Removes all candidates from the pool that have the same position as 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.
}

Part (a)

The getCandidatesForPosition method computes and returns a list of candidates for which the position matches position. If there are no candidates for which the position matches position, the method returns an empty list.

Complete method getCandidatesForPosition below.

/** Returns a list of candidates from the pool that have the same position as position
 *  @param position the position of candidates to return
 *  @return a list of candidates that have the desired position */
public List<Candidate> getCandidatesForPosition(String position)

Part (b)

The getBestCandidate method computes and returns the candidate with the highest interview score whose position matches position. If there is no candidate whose position matches position, the method returns null.

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

Complete method getBestCandidate below.

/** Returns the candidate from the pool with the highest interview score thathas the same position as position or null if position does not match theposition of any candidate.
 *  @param position the position of the candidate to return
 *  @return the candidate for position with the highest interview score or null */
public Candidate getBestCandidate(String position)

Part (c)

Method removeCandidatesForPosition updates the pool by removing all candidates for which the position matches position. The pool may contain zero or more candidates with the position position. The method returns the number of candidates removed from the pool.

Complete method removeCandidatesForPosition below.

/** Removes all candidates from the pool that have the same position as 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)

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

Get AP CS Help

One comment on “CandidatePool free response practice question

  1. Reply Kiara May 7,2012 7:01 pm

    Nice test

Leave a Reply