CandidatePool Problem

advertisement
CandidatePool free response practice question for ArrayLists on the AP Computer Science Exam
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)
Note from teacher to students: Make an initial attempt on a white piece of copy paper –
write out what you can of this method. As we are using this exercise for learning as
much as practice, below is some white text (that you can highlight and turn black to
view) that reflects some hints that you can use to either adjust or confirm the code you
initially wrote. Then please transcribe your final answer to this Google Form:
http://tinyurl.com/apjava009
After you submit your answer, you are welcome to go to the sub for the URL of the
solution page to this part of the problem.
Hints: ( In Java, an array list may be used to store several instances of an object, such
as Candidate. This method is to return an array list of candidates for a certain
position. Therefore, the first thing that needs to be done in the method is to declare
such a list (you could use lowercase "candidates" as the variable name for such a list).
The syntax is:
List<theNameOftheObjectsWithinTheList> varNameOfList = new ArrayList<theNameOftheObjectsWithinTheList>;
List<Candidate> candidates = new ArrayList<Candidate>;
Now that you have a list in which to add Candidates that are seeking the same position
that was specified in the method call, you just need to traverse the pool of candidates
and see if any of those in the pool are looking for the same position as what was in the
parameter. If so, add that candidate to your newly created list of candidates. After
all the candidates in the pool have been traversed, return the list you made.
Use a for each loop to traverse the pool:
for (Candidate candidate : pool) {
)
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 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)
Note from teacher to students: Make an initial attempt on a white piece of copy paper –
write out what you can of this method. As we are using this exercise for learning as
much as practice, below is some white text (that you can highlight and turn black to
view) that reflects some hints that you can use to either adjust or confirm the code you
initially wrote. Then please transcribe your final answer to this Google Form:
http://tinyurl.com/apjava010
After you submit your answer, you are welcome to go to the sub for the URL of the
solution page to this part of the problem.
Hints: ( Here, you need to first leverage the method from part A to obtain a list of
possible candidates. Once you have that list check to see if its size is zero:
candidates.size(). If it is, return null.
If the list is populated (has one or more object), you have to traverse this list of
possible candidates to find the one with the highest interview score. One way to do this
is to set the first object in the list as the bestCandidate.
Remember the built-in "get" method for lists, it allows you to "grab" an object from any
list:
Candidate bestCandidate = candidates.get(0); // first object is indexed at zero
Then just traverse the remaining objects – and if any of them scored higher, then set
that candidate as the bestCandidate.
To traverse the list from the second object to the last, use an index:
for (int i = 1; i < candidates.size(); i++) {
)
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)
Note from teacher to students: Make an initial attempt on a white piece of copy paper –
write out what you can of this method. As we are using this exercise for learning as
much as practice, below is some white text (that you can highlight and turn black to
view) that reflects some hints that you can use to either adjust or confirm the code you
initially wrote. Then please transcribe your final answer to this Google Form:
http://tinyurl.com/apjava011
After you submit your answer, you are welcome to go to the sub for the URL of the
solution page to this part of the problem.
Hints: ( If you aren't careful, funny things can happen when you traverse a list from the beginning to end – and try to remove
objects as you go. It is far simpler to traverse the list from the end to the beginning when you are looking to remove some of
the objects in a list. The built-in "remove" method can remove an object from a list: pool.remove(i)
for (int i = pool.size(); i >= 0; i--) {
if (pool.get(i).getPosition().equals(position)) {
)
Download