8.4.10. Free Response - Delimiters B

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

../_images/frq-delim-2019-q3b.png

8.4.10.2. 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. Click on a button to reveal a question.

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

7-4-10-3: Explain in plain English what your code will have to do to answer this question. Use the names given above.

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. Click on a button to reveal the algorithm or problem.

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

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

You have attempted of activities on this page