7.4.9. Free Response - Delimiters A

The following is a free response question from 2019. It was question 3 part A on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.

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

../_images/2019del1.png

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

../_images/2019del2.png

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

../_images/2019del3.png
../_images/2019del4.png

7.4.9.2. 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. Click the buttons to reveal the problems if you want to do them.

7-4-9-1: Click on the part of the sentences below with the type of thing passed to the method getDelimitersList and the type it returns.Variable declarations start with a type and then a name.
A string containing text and possibly delimiters has been split into *tokens* and stored inTest2
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.

    7-4-9-2: What type is tokens?

  • array
  • tokens is an array of Strings
  • List
  • Check again
  • String
  • Check again
  • ArrayList
  • Check again

    7-4-9-3: What type of thing is in tokens?

  • int
  • Check again.
  • String
  • Yes, tokens is an array of strings.
  • List
  • Check again.
  • double
  • Check again.

    7-4-9-4: What type of thing does getDelimitersList return?

  • int
  • Check again.
  • String
  • Check again.
  • ArrayList
  • It returns a list of strings, which is actually an ArrayList.
  • double
  • Check again.

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

7-4-9-5: Explain in plain English what your code will have to do to answer this question. Use the variable 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.

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.

    7-4-9-6: Which Java expression correctly creates an empty ArrayList of type String called delList?

  • delList = new ArrayList<String>();
  • You must declare the type for delList
  • ArrayList<String> delList = new ArrayList<String>;
  • You must include the () when creating a new object
  • ArrayList<String> delList = new List<String>();
  • You must create an ArrayList using a concrete subclass like ArrayList
  • ArrayList<String> delList = new ArrayList<String>();
  • The declared type must be the same or a parent class of the actual type.

    7-4-9-7: Which loop would be best for this situation?

  • while
  • You can use a while loop, but it would make your code more error prone than another type of loop
  • for
  • You can use a for loop, but it would make your code more error prone than another type of loop
  • for-each
  • Since you need to loop through all the strings in the array tokens in order, a for-each loop would be best
  • nested for loop
  • There is no need for a nested loop in this situation

    7-4-9-8: Which code adds item to the end of the list called delList?

  • delList.set(0,item);
  • This would change the value at index 0 to item.
  • delList.add(0,item);
  • This would add item at index 0 and move right any other items in the list
  • delList.remove(item);
  • This would remove item from the list
  • delList.add(item);
  • This adds item to the end of the list

    7-4-9-9: Which code correctly checks if token is equal to (has the same characters as) openDel or closeDel?

  • if (token == openDel && token == closeDel)
  • You should use .equals with strings and || for or
  • if (token == openDel || token == closeDel)
  • You should use .equals with strings
  • if (token.equals(openDel) && token.equals(closeDel))
  • You should use || for or not &&
  • if (token.equals(openDel) || token.equals(closeDel))
  • This returns true when openDel or closeDel have the same characters as token

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

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

You have attempted of activities on this page