# FRQ 1 MessageBuilder with Loops and Strings

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 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.

FRQ Question 1 on Control Structures will probably involve:

- a `for` or `while` loop that probably uses the method’s parameter variables,
- an `if statement`, probably inside the loop,
- calls to other class `methods given to you`,
- a numerical or string value that is calculated by the loop and `returned` at the end of the method.
- Part A will require a loop and part B will require String methods (with or without a loop).

On question 1, you will get points for:

- constructing the loop correctly,
- calling the correct methods,
- calculating the correct value,
- and returning the value.

Try to have some code for each of these steps. Do not use arrays or other more complex code. You may need to use Math and String methods.

## FRQ 1 MessageBuilder

The following free response question is a great FRQ 1 example from page 168 of the [2025-2026 AP CSA Course and Exam Description](https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf).

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.

```java
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. */
}
```

## Part A: MessageBuilder Constructor

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.

| Method Call | Return Value |
| --- | --- |
| `getNextWord("the")` | `"book"` |
| `getNextWord("book")` | `"on"` |
| `getNextWord("on")` | `"the"` |
| `getNextWord("the")` | `"table"` |
| `getNextWord("table")` | `null` |

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`.

**Example 2:** Now consider a different set of calls to `getNextWord`. Again, the return value of each call is used as input to the next.

| Method Call | Return Value |
| --- | --- |
| `getNextWord("good")` | `"morning"` |
| `getNextWord("morning")` | `"sunshine"` |
| `getNextWord("sunshine")` | `null` |

Based on this, a call to the `MessageBuilder` constructor with argument `"good"` should set `message` to `"good morning sunshine"` and `numWords` to `3`.

## How to solve the MessageBuilder constructor

Here are some exercises to help you to tackle this FRQ. First, identify and highlight the methods and variables that you must use in the problem.

- The parameter `startingWord`
- The helper method `getNextWord(String s)`
- The instance variables `message` and `numWords`

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.

Now let's plan the loop. The following questions will help guide your thinking.

> Check your understanding: Which loop structure is best suited to keep building the message until `getNextWord` returns `null`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQmessagebuilder.html#msgbuilder-loop-type

> Check your understanding: What is the purpose of the variable `nextWord` in the loop? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQmessagebuilder.html#msgbuilder-variables

As you write the loop, remember the 3 steps of the loop:

1. Initialize the loop variable `nextWord` using the `getNextWord` method with the starting word as an argument.
2. Test the loop variable to make sure it is not `null`.
3. Change the loop variable to the next word using the getNextWord method with the last word as an argument.

## Solve MessageBuilder Part A

Complete the `MessageBuilder` constructor below.

### Exercise 1

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.

> Open the `01-FRQmessageBuilderPartA` folder and edit `MessageBuilder.java`. Press **Run** to try it; **Run Tests** checks it (4 tests).

## AP Scoring Rubric for Part A

Here is the AP rubric for Part A. 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.

![AP Rubric](images/frq-scoring-msgbuilder-parta.png)

*AP Rubric*

## Part B: getAbbreviation

Starting in 2026, the AP exam will require students to use String methods in part B of FRQ 1.

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"`.

> Check your understanding: Which of the following String method can be used to return the first letter of a word? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQmessagebuilder.html#msgbuilder-string-methods

> Check your understanding: Which of the following String method can be used to find a space character in the message? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQmessagebuilder.html#msgbuilder-string-methods2

> Check your understanding: Which of the following loop patterns can be used to find the next space in a String? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQmessagebuilder.html#msgbuilder-partb-loop

## Solve MessageBuilder Part B

### Exercise 2

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.

> Open the `02-FRQmessageBuilderPartB` folder and edit `MessageBuilder.java`. Press **Run** to try it; **Run Tests** checks it (2 tests).

## AP Scoring Rubric

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.

![AP Rubric](images/frq-scoring-msgbuilder-partb.png)

*AP Rubric*

## More FRQ1 Practice Problems

Try additional FRQ 1 practice problems with ifs and loops from the Oracle learn.java site:

[https://learn.java/teachers/apcsa/](https://learn.java/teachers/apcsa/)
