RandomStringChooser - Part A

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise2 graded tests

Files

README.mdDownload

RandomStringChooser - Part A

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

The following is a free response question from 2016. It was question 1 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.

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

Part 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++)
{
   System.out.println(sChooser.getNext() + " ");
}

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

bus the wheels on NONE NONE

Try and Solve It

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 or any of the methods should alter the parameter passed to the constructor, but your implementation may copy the contents of the array.

Exercise 1

Complete the RandomStringChooser class.

Open the 01-codeRandomStrChooserA folder and edit Main.java. Press Run to try it; Run Tests checks it (2 tests). Name the class, then rename the file to match.

For teachers

RandomStringChooser - Part A

Students read CSAwesome2's “RandomStringChooser - Part A” in CS Room and work through its 1 Java coding exercise.

  • Time: 1 class period
  • Tests: 2 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for the AP exam's free-response questions.

Before class

In class

  1. Students read README.md.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-codeRandomStrChooserAMain.javaAutograded2

Teachers can download a reference solution after signing in.