Unit 4B - Free Response - Delimiters A

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise6 graded tests

Files

README.mdDownload

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.

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

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

2019del2

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

2019del4

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

For teachers

Unit 4B - Free Response - Delimiters A

Students read CSAwesome2's “Unit 4B - Free Response - Delimiters A” in CS Room and work through its 1 Java coding exercise.

  • Time: 1 class period
  • Tests: 6 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-frq2019Q3A-delimDelimiters.javaAutograded6

Teachers can download a reference solution after signing in.