AP Comp Sci A Practice FRQ 2
AP Comp Sci A Practice FRQ 2
/** Returns kth word in alphabetical order, where 1 <= k <= size().
* @param k position of word to be returned
* @return the kth word
*/
public String findkth(int k)
{ /* implementation not shown */ }
The findkth method returns the kth word in alphabetical order in the set, even
though the implementation of WordSet may not be sorted. The number k
ranges from 1 (corresponding to first in alphabetical order) to N , where N is
the number of words in the set. For example, if WordSet s stores the words
{"GRAPE", "PEAR", "FIG", "APPLE"}, here are the values when s.findkth(k)
is called.
k values of s.findkth(k)
1 APPLE
2 FIG
3 GRAPE
4 PEAR
(a) Write a client method countA that returns the number of words in WordSet
s that begin with the letter “A.” In writing countA, you may call any of the
methods of the WordSet class. Assume that the methods work as specified.
Practice Exam 2
public static int countA(WordSet s)
(b) Write a client method removeA that removes all words that begin with “A.”
If there are no such words in s, then removeA does nothing. In writing
removeA, you may call method countA specified in part (a). Assume that
countA works as specified, regardless of what you wrote in part (a).
public WordSet()
public int size()
public void insert(String word)
public void remove(String word)
public String findkth(int k)
public boolean contains(String word)
(c) Write a client method commonElements that returns the WordSet containing
just those elements occurring in both of its WordSet parameters.
For example, if s1 is {"BE", "NOT", "AFRAID"} and s2 is {"TO", "BE",
"OR", "NOT"}, then commonElements(s1, s2) should return the WordSet
{"BE", "NOT"}. (If you are familiar with mathematical set theory,
commonElements returns the intersection of s1 and s2.)
Complete method commonElements below.
0 1 2 CONTESTANTS_PER_ROW-1
0 ···
1 ···
.
. .
. .
. .
.
. . . .
NUM_ROWS-1 ···
Since contestants may be moved around during the competition, each contestant
keeps track of his or her location, which is the row number and column number.
A Location object is represented by the class below.