0% found this document useful (0 votes)
3 views4 pages

Java Random String Chooser Guide

The document outlines a Computer Science exam section focused on Java programming, consisting of four questions that account for 50% of the total score. It includes instructions for implementing a RandomStringChooser class and a subclass called RandomLetterChooser, detailing their expected behaviors and methods. Students are required to show all work and adhere to provided guidelines while writing their solutions in Java.

Uploaded by

chandan.dabburi
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)
3 views4 pages

Java Random String Chooser Guide

The document outlines a Computer Science exam section focused on Java programming, consisting of four questions that account for 50% of the total score. It includes instructions for implementing a RandomStringChooser class and a subclass called RandomLetterChooser, detailing their expected behaviors and methods. Students are required to show all work and adhere to provided guidelines while writing their solutions in Java.

Uploaded by

chandan.dabburi
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

COMPUTER SCIENCE A

SECTION II
Time—1 hour and 30 minutes
Number of questions—4
Percent of total score—50

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.

Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.

1. This question involves the implementation and extension of a RandomStringChooser class.

(a) A RandomStringChooser object is constructed from an array of non-null String values. When
the object is first constructed, all of the strings are considered available. The RandomStringChooser
class has a getNext method, which has the following behavior. A call to getNext returns a randomly
chosen string from the available strings in the object. Once a particular string has been returned from
a call to getNext, it is no longer available to be returned from subsequent calls to getNext. If no
strings are available to be returned, getNext returns "NONE".
The following code segment shows an example of the behavior of RandomStringChooser.

String[] wordArray = {"wheels", "on", "the", "bus"};


RandomStringChooser sChooser = new RandomStringChooser(wordArray);
for (int k = 0; k < 6; k++)
{
[Link]([Link]() + " ");
}

One possible output is shown below. Because sChooser has only four strings, the string "NONE" is
printed twice.

bus the wheels on NONE NONE

WRITE YOUR SOLUTION ON THE NEXT PAGE.

© 2016 The College Board.


Visit the College Board on the Web: [Link].

GO ON TO THE NEXT PAGE.


-2-
Write the entire RandomStringChooser class. Your implementation must include an appropriate
constructor and any necessary methods. Any instance variables must be private. The code segment in
the example above should have the indicated behavior (that is, it must compile and produce a result like the
possible output shown). Neither the constructor nor any of the methods should alter the parameter passed to
the constructor, but your implementation may copy the contents of the array.

Part (b) begins on page 4.

© 2016 The College Board.


Visit the College Board on the Web: [Link].

GO ON TO THE NEXT PAGE.


-3-
(b) The following partially completed RandomLetterChooser class is a subclass of the
RandomStringChooser class. You will write the constructor for the RandomLetterChooser
class.

public class RandomLetterChooser extends RandomStringChooser


{
/** Constructs a random letter chooser using the given string str.
* Precondition: str contains only letters.
*/
public RandomLetterChooser(String str)
{ /* to be implemented in part (b) */ }

/** Returns an array of single-letter strings.


* Each of these strings consists of a single letter from str. Element k
* of the returned array contains the single letter at position k of str.
* For example, getSingleLetters("cat") returns the
* array { "c", "a", "t" }.
*/
public static String[] getSingleLetters(String str)
{ /* implementation not shown */ }
}

The following code segment shows an example of using RandomLetterChooser.

RandomLetterChooser letterChooser = new RandomLetterChooser("cat");


for (int k = 0; k < 4; k++)
{
[Link]([Link]());
}

The code segment will print the three letters in "cat" in one of the possible orders. Because there are
only three letters in the original string, the code segment prints "NONE" the fourth time through the loop.
One possible output is shown below.

actNONE

© 2016 The College Board.


Visit the College Board on the Web: [Link].

GO ON TO THE NEXT PAGE.


-4-
Assume that the RandomStringChooser class that you wrote in part (a) has been implemented
correctly and that getSingleLetters works as specified. You must use getSingleLetters
appropriately to receive full credit.
Complete the RandomLetterChooser constructor below.

/** Constructs a random letter chooser using the given string str.
* Precondition: str contains only letters.
*/
public RandomLetterChooser(String str)

© 2016 The College Board.


Visit the College Board on the Web: [Link].

GO ON TO THE NEXT PAGE.


-5-

You might also like