0% found this document useful (0 votes)
904 views

AP Comp Sci A Practice FRQ 2

The WordSet class stores a set of capitalized string words with no duplicates and in no particular order. It provides methods to get the size, insert words, remove words, find the kth word alphabetically, and check if it contains a word. The document provides examples and tasks to (a) count words starting with "A", (b) remove words starting with "A", and (c) return the words that are common to two WordSet objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
904 views

AP Comp Sci A Practice FRQ 2

The WordSet class stores a set of capitalized string words with no duplicates and in no particular order. It provides methods to get the size, insert words, remove words, find the kth word alphabetically, and check if it contains a word. The document provides examples and tasks to (a) count words starting with "A", (b) remove words starting with "A", and (c) return the words that are common to two WordSet objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

412 Practice Exams

2. A WordSet, whose partial implementation is shown in the class declaration below,


stores a set of String objects in no particular order and contains no duplicates.
Each word is a sequence of capital letters only.

public class WordSet


{
/** Constructor initializes set to empty. */
public WordSet()
{ /* implementation not shown */ }

/** @return the number of words in set */


public int size()
{ /* implementation not shown */ }

/** Adds word to set (no duplicates).


* @param word the word to be added
*/
public void insert(String word)
{ /* implementation not shown */ }

/** Removes word from set if present, else does nothing.


* @param word the word to be removed
*/
public void remove(String word)
Practice Exam 2

{ /* implementation not shown */ }

/** 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 */ }

/** @return true if set contains word, false otherwise */


public boolean contains(String word)
{ /* implementation not shown */ }

//Other instance variables, constructors, and methods are not shown.


}

GO ON TO THE NEXT PAGE.


Practice Exam Two 413

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.

Complete method countA below.

/** @param s the current WordSet


* @return the number of words in s that begin with "A"
*/

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).

Information repeated from the beginning of the question

public class WordSet

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)

Complete method removeA below:

/** @param s the current WordSet


* Postcondition: WordSet s contains no words that begin with
* "A", but is otherwise unchanged.
*/
public static void removeA(WordSet s)

GO ON TO THE NEXT PAGE.


414 Practice Exams

(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.

/** @param s1 the first given set


* @param s2 the second given set
* @return the WordSet containing only the elements that occur
* in both s1 and s2
*/
public static WordSet commonElements(WordSet s1, WordSet s2)

3. A puzzle-solving competition is held in a large hall with a two-dimensional


arrangement of contestants. Each square below represents one contestant.
Practice Exam 2

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.

GO ON TO THE NEXT PAGE.

You might also like