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. */
}Example 1
| Method Call | Return Value |
|---|---|
| getNextSentence("The cat sat.") | "It looked down." |
| getNextSentence("It looked down.") | "It saw a mouse." |
| getNextSentence("It saw a mouse.") | null |
/** * 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)
Example 1
| Method Call | Return Value |
|---|---|
| countKeyword("an") | 4 |
| countKeyword("ana") | 3 |
| countKeyword("nana") | 1 |
/** * 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)