Unit 4B - Free Response - Delimiters B
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise5 graded tests
Files
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.
- When traversing the
ArrayListfrom 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. - 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.

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.
- When traversing the
ArrayList<String> delimitersfrom 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. - 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.
- When traversing the
ArrayListfrom 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. - 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-delimfolder and editDelimiters.java. Press Run to try it; Run Tests checks it (5 tests).
For teachers
Unit 4B - Free Response - Delimiters B
Students read CSAwesome2's “Unit 4B - Free Response - Delimiters B” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 5 in 1 graded exercise, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page, with its free-response and multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/delimitersQ3b.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students do exercise 01 in its folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-frq2019Q3B-delim | Delimiters.java | Autograded | 5 |
Teachers can download a reference solution after signing in.