Section2.25FRQ 1 MessageBuilder with Loops and Strings
The AP examβs first free response question (FRQ) is on Methods and Control Structures using expressions, Strings, loops, and if statements. Students will write two methods or one constructor and one method of a given class based on provided specifications and examples. In Part A (4 points),the method or constructor will require students to write iterative or conditional statements, or both, as well as statements that call methods in the specified class. In Part B (3 points), the method or constructor will require calling String methods as of 2026. The AP exam provides the method header with some parameter variables and other methods that you will need to call in the solution. This question does not involve more complex topics such as arrays.
This question involves the MessageBuilder class, which is used to generate a message based on a starting word. The MessageBuilder class contains a helper method, getNextWord. You will write a constructor and a method in the MessageBuilder class.
public class MessageBuilder
{
private String message; // To be initialized in part (a)
private int numWords; // To be initialized in part (a)
/**
* Builds a message starting with the word specified by the
* parameter and counts the number of words in the message,
* as described in part (a)
* Precondition: startingWord is a single word with no spaces.
*/
public MessageBuilder(String startingWord)
{ /* to be implemented in part (a) */ }
/**
* Returns a word to follow the word specified by the
* parameter or null if there are no remaining words.
* Precondition: s is a single word with no spaces.
* Postcondition: Returns an individual word with no spaces.
*/
public String getNextWord(String s)
{ /* implementation not shown */ }
/**
* Returns an abbreviation for the instance variable message,
* as described in part (b)
* Preconditions: Each word in message is separated by a single space.
* message contains two or more words.
* Postcondition: message is unchanged.
*/
public String getAbbreviation()
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods that are not shown. */
}
In Part A, you will write the MessageBuilder constructor, which uses a helper method to create a message starting with the word specified by the parameter startingWord and assigns the message to the instance variable message. The constructor also counts the number of words in the message and assigns the count to the instance variable numWords.
Each word in the message will be separated by a single space. A helper method, getNextWord, has been provided to obtain words to be added to the message. Each call to getNextWord returns the next word in the message based on the word most recently added. When there are no more words to be added, getNextWord returns null.
Example 1: Consider the following calls to getNextWord, which are made within the MessageBuilder class. The return value of each call is used in the next call.
Based on these return values, a call to the MessageBuilder constructor with argument "the" should set the instance variable message to "the book on the table" and should set the instance variable numWords to 5.
We will learn more about writing constructors in the next unit, but for now, you can think of a constructor as a method that is called when an object is created to initialize the instance variables of the class, in this case message and numWords.
You will write a loop that adds to the message variable using the getNextWord method and increments numWords as you add more words to the message. The constructor starts with startingWord and repeatedly adds the result of getNextWord until it returns null. Each word is separated by a space.
You can declare a temporary variable such as nextWord as your loop variable to store the result of getNextWord, and update message and numWords in each loop iteration.
Complete the MessageBuilder constructor below that builds a message starting with the startingWord specified by the parameter. It should use a while loop to add on to the message using the given getNextWord method and increment numWords. Make sure there are spaces between the words in the message.
In MessageBuilder Part B, you are asked to write the getAbbreviation method, which returns an abbreviation consisting of the first letter of each word in message. Assume that message consists of two or more words, each separated by a single space. For example, if the value of message is "as soon as
possible", then getAbbreviation() should return "asap".
This iterates through the message letter by letter, but it doesnβt find the next space. You could make it work with an if statement inside to find the space, but there is another solution.
You can use indexOf(" ") to find the index of a space in a string easily. You can copy message into tempMessage so that you can change it to skip to the next word.
Here is the AP rubric for this problem. Did your code meet the requirements for all 4 points? Notice that even a partial solution would get some of the points. It is not all or nothing. In class, your teacher may have you grade each othersβ code.
Complete the getAbbreviation() method below which returns a String that consists of the first letter of each word in the message. Assume that message consists of two or more words, each separated by a single space.
Complete the getAbbreviation() method below that returns a String that consists of the first letter of each word in the message. Assume that message consists of two or more words, each separated by a single space.
Here is the AP rubric for this problem. Did your code meet the requirements for all 7 points? Notice that even a partial solution would get some of the points. It is not all or nothing. There are also many other solutions possible. In class, your teacher may have you grade each othersβ code.