# Unit 4B - Free Response - Delimiters A

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 following is a free response question from 2019. It was question 3 part A on the exam [https://apcentral.collegeboard.org/media/pdf/ap19-frq-computer-science-a.pdf](https://apcentral.collegeboard.org/media/pdf/ap19-frq-computer-science-a.pdf).

Many encoded strings contain *delimiters*. A delimiter is a non-empty string that acts as a boundary between different parts of a larger string. The delimiters involved in this question occur in pairs that must be *balanced*, with each pair having an open delimiter and a close delimiter. There will be only one type of delimiter for each string. The following are examples of delimiters.

![2019del1](images/2019del1.png)

In this question, you will write two methods in the following `Delimiters` class.

![2019del2](images/2019del2.png)

## Part A

(a) A string containing text and possibly delimiters has been split into *tokens* and stored in `String[] tokens`. Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter. You will write the method `getDelimitersList`, which returns an `ArrayList` containing all the open and close delimiters found in `tokens` in their original order.

The following examples show the contents of an `ArrayList` returned by `getDelimitersList` for different open and close delimiters and different `tokens` arrays.

![2019del3](images/2019del3.png)

![2019del4](images/2019del4.png)

## Check your understanding of the question

There are problems in this section that can help you check your understanding of the question. You can skip these if you think you know what to do already.

> Check your understanding: Click on the part of the sentences below with the type of thing passed to the method getDelimitersList  and the type it returns.
>
> ```
> A string containing text and possibly delimiters has been split into *tokens*
> and stored in Test2 String[] tokens.
> Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter.
> You will write the method getDelimitersList, which returns an ArrayList
>  containing all the open and close delimiters found in tokens in their original order.
> ```
>
> Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_find_parts_ca

> Check your understanding: What type is tokens? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_cu_mc_1

> Check your understanding: What type of thing is in tokens? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_cu_mc_2

> Check your understanding: What type of thing does getDelimitersList return? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_cu_mc_3

## How to Solve Part A

Here is the question again.

A string containing text and possibly delimiters has been split into *tokens* and stored in `String[] tokens`. Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter. You will write the method `getDelimitersList`, which returns an `ArrayList` containing all the open and close delimiters found in `tokens` in their original order.

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/delimitersQ3a.html#delim_algorithm

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.

The method `getDelimtersList` needs to return an `ArrayList` of `Strings` containing all the open and close delimiters found in the `tokens` array in their original order.

This implies that the code needs to create an empty `ArrayList` of type `String`. Let’s call it `delList`. The code will loop through the strings in the array `tokens` from the start to the end and if the current string is equal to either the `openDel` or `closeDel` it adds that string to the end of delList. Finally it should return delList.

> Check your understanding: Which Java expression correctly creates an empty ArrayList of type String called delList? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_solve_mc_1

> Check your understanding: Which loop would be best for this situation? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_solve_mc_2

> Check your understanding: Which code adds item to the end of the list called delList? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_solve_mc_3

> Check your understanding: Which code correctly checks if token is equal to (has the same characters as) openDel or closeDel? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3a.html#delim_solve_mc_4

## Write the Code

A string containing text and possibly delimiters has been split into *tokens* and stored in `String[] tokens`. Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter. You will write the method `getDelimitersList`, which returns an `ArrayList` containing all the open and close delimiters found in `tokens` in their original order.

### Exercise 1

Write the method getDelimitersList in the code. The main method contains code to test your solution.

> Open the `01-frq2019Q3A-delim` folder and edit `Delimiters.java`. Press **Run** to try it; **Run Tests** checks it (6 tests).
