Fiveable
๐Ÿ’ปAP Computer Science A
โ€‹
FRQ 1 โ€“ Methods and Control Structures
โ€‹
Unit 1: Using Objects and Methods
โ€‹
Practice FRQ 1 of 19
1. This question involves the StoryBuilder class, which is used to construct a story by chaining sentences together. The StoryBuilder class uses a helper method, getNextSentence, to determine the flow of the story. You will write a constructor and a method in the StoryBuilder class.

java

public class StoryBuilder
{
    private String story; // To be initialized in part (a)
    private int numSentences; // To be initialized in part (a)

    /**
     * Constructs a StoryBuilder by building a story starting with
     * firstSentence and counts the number of sentences in the story,
     * as described in part (a)
     * Precondition: firstSentence is not null and does not end with a space.
     */
    public StoryBuilder(String firstSentence)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the next sentence based on the previous sentence,
     * or null if the story should end.
     * Precondition: prevSentence is not null.
     */
    public String getNextSentence(String prevSentence)
    { /* implementation not shown */ }

    /**
     * Returns the number of times the keyword appears in the story,
     * as described in part (b)
     * Precondition: keyword is not null and has length > 0.
     */
    public int countKeyword(String keyword)
    { /* to be implemented in part (b) */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
a. Write the StoryBuilder constructor, which initializes the instance variable story to the parameter firstSentence and initializes the instance variable numSentences to 1. The constructor should then repeatedly call the helper method getNextSentence to obtain the next sentence to add to the story. The argument passed to getNextSentence should be the sentence most recently added to the story. If getNextSentence returns a non-null string, the constructor should append a space followed by that string to story and increment numSentences. The process stops when getNextSentence returns null.
A helper method, getNextSentence, has been provided. Consecutive calls to getNextSentence are guaranteed to eventually return null, ensuring the constructor terminates.

Example 1

Consider the following calls to getNextSentence, which are made within the StoryBuilder class. The return value of each call is used in the next call.
Method CallReturn Value
getNextSentence("The cat sat.")"It looked down."
getNextSentence("It looked down.")"It saw a mouse."
getNextSentence("It saw a mouse.")null
Based on these return values, a call to the StoryBuilder constructor with argument "The cat sat." should set the instance variable story to "The cat sat. It looked down. It saw a mouse." and should set the instance variable numSentences to 3.
Complete the StoryBuilder constructor. You must use getNextSentence appropriately in order to receive full credit.
/**
 * Constructs a StoryBuilder by building a story starting with
 * firstSentence and counts the number of sentences in the story,
 * as described in part (a)
 * Precondition: firstSentence is not null and does not end with a space.
 */
public StoryBuilder(String firstSentence)
b. Write the countKeyword method, which returns the number of times the parameter keyword appears in the instance variable story. Occurrences of keyword may overlap.

Example 1

Assume that the instance variable story contains the string "banana bandana".
Method CallReturn Value
countKeyword("an")4
countKeyword("ana")3
countKeyword("nana")1
In "banana bandana", "an" appears 4 times (indices 1, 3, 8, 11). "ana" appears 3 times (indices 1, 3, 11), demonstrating that overlaps are counted.
Complete the countKeyword method.
/**
 * Returns the number of times the keyword appears in the story,
 * as described in part (b)
 * Precondition: keyword is not null and has length > 0.
 */
public int countKeyword(String keyword)






Pep