# FRQ3 - ArrayLists - Part 2

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.

From the 2025 Course and Exam Description:

**Free-Response Question 3: Data Analysis with ArrayList**. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze, and manipulate data in an ArrayList structure.

FRQ 3 requires the following skills:

- Iterate through an ArrayList
- Access elements of an ArrayList
- Call methods on elements of the ArrayList
- Use a provided class

## 2014 FRQ3 - ScrambledStrings

The following is part b of a free response question from 2014. It was question 1 on the exam. You can see all the free response questions from past exams at [Past AP CSA Exams](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/) .

This question involves reasoning about strings made up of uppercase letters. You will implement two related methods that appear in the same class (not shown). The first method takes a single string parameter and returns a scrambled version of that string. The second method takes a list of strings and modifies the list by scrambling each entry in the list. Any entry that cannot be scrambled is removed from the list.

**Part b.** Write the method `scrambleOrRemove`, which replaces each word in the parameter `wordList` with its scrambled version and removes any words that are unchanged after scrambling. The relative order of the words in `wordList` remains the same as before the call to `scrambleOrRemove`.

The following example shows how the contents of `wordList` would be modified as a result of calling `scrambleOrRemove`.

![Example call and result](images/stringScrambleB.png)

*Example call and result*

Assume that the method `scrambleWord` works as intended and is in the same class. It will return the scrambled word or the same word. You will write the `scrambleOrRemove` method to replace each original word with the scrambled word or remove the word if it was not scrambled.

```java
import java.util.List;

public class ScrambledStrings
{

    /**
     * Modifies wordList by replacing each word with its scrambled version,
     * removing any words that are unchanged as a result of scrambling.
     *
     * @param wordList the list of words Precondition: wordList contains only
     *     non-null objects Postcondition: - all words unchanged by scrambling have
     *     been removed from wordList - each of the remaining words has been
     *     replaced by its scrambled version - the relative ordering of the entries
     *     in wordList is the same as it was before the method was called
     */
    public static void scrambleOrRemove(List<String> wordList)
    {
        /* to be implemented in part b */
    }
}
```

## How to solve this problem

Explain in plain English what your code will have to do to answer this question. Use the variable names given above.

> Discuss this with your class, or answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#string_algorithm_partB

This section contains a plain English explanation of one way to solve this problem as well as problems that test your understanding of how to write the code to do those things.

In the example the first word (at index 0) `TAN` is scrambled and replaced. The second word `ABRACADABRA` (at index 1) is scrambled and replaced. The third word `WHOA` (at index 2) is removed. The fourth word `APPLE` (at index 3) is scrambled and replaced. The fifth word `EGGS` (at index 4) is removed since the scrambled word is the same as the original. What method of List allows you to replace an element in a list? What method of list allows you to remove an element from a list? How can you loop through a list and not always increment the current index?

> Check your understanding: What is returned by this method? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_0

> Check your understanding: This method accepts an `ArrayList`, what is the datatype of the objects contained in this `ArrayList`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_0_5

> Check your understanding: There are many ways to use loops to solve this problem. If we were to use a while loop, what conditional could we write to make sure the loop does not go out of bounds? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_1

> Check your understanding: You can also use a for loop to solve this problem instead of a while loop. what conditional could we write to make sure the loop does not go out of bounds? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_2

> Check your understanding: How would you access each element in wordList assuming you already have an integer index properly initialized. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_3

> Check your understanding: How would you check that a string `word` is equal to a different string called `other`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#strng_mcalg_answr_4

## The Algorithm

Loop through the list and scramble the current word. If the scrambled word and original are equal then remove the word from the list and otherwise replace it. We will have to be careful since the size of the list can change in the loop. If we remove an element all the other elements will shift left. We will only want to increment the index if the word was replaced and not removed. There are many ways to solve this problem but we have outlined 2 in the following optional questions. If you feel that you are ready to solve the problem, please skip ahead to the exercise block.

```java
public static void scrambleOrRemove(List<String> wordList)
    initialize index counter
    while (index less than wordlist size)
      initialize a string and set it equal to word in wordList at index

      initialize another string and set it equal to the scrambled version
      of the word in wordlist at index

      if (the normal string equals the scrambled string)
          remove the word in wordList at the current index
      else
          reassign the current word in wordList to be the scrambled version
          iterate the index
```

> Check your understanding: The method test below contains the correct code for one solution to this problem, but it is mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#StringScrambleB1

Another way to solve this problem is to start at the end of the list and loop towards the front of the list. That way you don’t have to worry about the index being off if you remove an item from the list.

```java
public static void scrambleOrRemove(List<String> wordList)
      for( int i = wordList size - 1; i >= 0; i--)
          initialize a string and set it equal to word in wordList at index

          initialize another string and set it equal to the scrambled version
          of the word in wordlist at index

          if (the normal string equals the scrambled string)
              remove the word in wordList at the current index
          else
              reassign the current word in wordList to be the scrambled version
              iterate the index
```

> Check your understanding: The method test below contains the correct code for another solution to this problem, but it is mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQ3-Part2.html#StringScrambleB2

## Try and Solve It

### Exercise 1

Write the method `scrambleOrRemove`. The main has code to test the result.

> Open the `01-frqScrambleOrRemove` folder and edit `ScrambledStrings.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).
