RandomStringChooser - Part B
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise2 graded tests
Files
RandomStringChooser - Part B
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.
Part 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") return 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++)
{
System.out.print(letterChooser.getNext());
}
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 posible output is shown below.
actNONE
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. The following code block shows the construtor declaration.
/** Constructs a random letter chooser using the given string str.
* Precondition: str contains only letters.
*/
public RandomLetterChooser(String str)
Try and Solve It
Complete the RandomLetterChooser constructor below Copy in your RandomStringChooser class from the previous lesson (delete the public from in front of it since there can only be 1 public class if you have more than one).
The code below has a main method for testing. Write the constructor for the RandomLetterChooser class and use the main method to test it.
Exercise 1
Complete the RandomLetterChooser constructor below (which can be 1 line of code). Copy in your RandomStringChooser class from the previous lesson (delete the public from in front of it since there can only be 1 public class if you have more than one).
Open the
01-RandomStrChooserB1folder and editRandomLetterChooser.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
RandomStringChooser - Part B
Students read CSAwesome2's “RandomStringChooser - Part B” 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
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/RandomStringChooserB.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercise 01 in its folder, pressing Run to try their code.
- 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
| Folder | File | Grading | Tests |
|---|---|---|---|
01-RandomStrChooserB1 | RandomLetterChooser.java | Autograded | 2 |
Teachers can download a reference solution after signing in.