# Unit 4B - Free Response - Delimiters B

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.

## Part B

(b) Write the method `isBalanced`, which returns `true` when the delimiters are balanced and returns `false` otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.

1. When traversing the `ArrayList` from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
2. the total number of open delimiters is equal to the total number of close delimiters.

Consider a `Delimiters` object for which `openDel` is `"<sup>"` and `closeDel` is `"</sup>"`. The examples below show different `ArrayList` objects that could be returned by calls to `getDelimitersList` and the value that would be returned by a call to `isBalanced`.

![Frq delim 2019 q3b](images/frq-delim-2019-q3b.png)

## Check your understanding of the Question

The problems in this section can help you check your understanding of part B. You can skip these if you think you know what to do already.

> Check your understanding: What type does isBalanced return? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3b.html#delim_cub_mc_1

> Check your understanding: What is the name of the variable the code will be looping through? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3b.html#delim_cub_mc_2

## How to Solve Part B

Here is the question again.

Write the method `isBalanced`, which returns `true` when the delimiters are balanced and returns `false` otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.

1. When traversing the `ArrayList<String> delimiters` from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
2. the total number of open delimiters is equal to the total number of close delimiters.

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

> Discuss this with your class, or answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3b.html#delim_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.

The method `isBalanced` will loop through `delimiters` and keep track of the number of open and close delimiters we have found so far. To do that we can create two integer variables: `totalOpen` and `totalClose` and set them to 0 initially. Each time through the loop we will check if the current string which we will call currString is equal to `openDel` and if so increment `totalOpen`, otherwise if it is equal to `closeDel` increment `totalClose`. Next if `totalClose` > `totalOpen` the method should return false. A After the loop return `totalOpen` == `totalClose`. This will return true if they are equal and false otherwise.

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

> Check your understanding: Which Java expression correctly tests if currString is equal to openDel? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3b.html#delim_solveb_mc_2

## Write the Code

Write the method `isBalanced`, which returns `true` when the delimiters are balanced and returns `false` otherwise. The delimiters are balanced when both of the following conditions are satisfied; otherwise they are not balanced.

1. When traversing the `ArrayList` from the first element to the last element, there is no point at which there are more close delimiters than open delimiters at or before that point.
2. the total number of open delimiters is equal to the total number of close delimiters.

### Exercise 1

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

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