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.
Figure4.43.1.
In this question, you will write two methods in the following Delimiters class.
Figure4.43.2.
Subsection4.43.1Part 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.
Figure4.43.3.
Figure4.43.4.
Subsection4.43.2Check 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.
Activity4.43.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 stringcontaining text and possibly delimiters has been split into *tokens*and stored in Test2String[] tokens.
Each token is either an open delimiter, a close delimiter, or a substring that is not a delimiter.You will write the methodgetDelimitersList,which returns anArrayListcontaining all the open and close delimiters found in tokens in their original order.
Activity4.43.2.
What type is tokens?
array
tokens is an array of Strings
List
Check again
String
Check again
ArrayList
Check again
Activity4.43.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.
Activity4.43.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.
Subsection4.43.3How 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.
Activity4.43.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.
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.
Activity4.43.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.
Activity4.43.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
Activity4.43.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
Activity4.43.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
Subsection4.43.4Write 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.
Activity4.43.10.
Write the method getDelimitersList in the code below. The main method contains code to test your solution.