Computer Science A
2024 FRQ 3
- 2024 AP Computer Science A
- Question 3: WordChecker - ArrayList String Manipulation
- Code Runner Challenge
- (a) Write the
isWordChainmethod- Code Runner Challenge
- (b) Write the
createListmethod- Code Runner Challenge
2024 AP Computer Science A
Free Response Question 3: WordChecker Class
Question 3: WordChecker - ArrayList String Manipulation
This question involves the manipulation and analysis of a list of words. The WordChecker class contains an ArrayList<String> to be analyzed and methods that are used to perform the analysis. You will write two methods of the WordChecker class.
Code Runner Challenge
The WordChecker class contains an ArrayList and methods to analyze word patterns.
(a) Write the isWordChain method
Write the isWordChain method, which determines whether each element of wordList (except the first) contains the previous element as a substring. The following table shows two sample isWordChain method calls.
| wordList | isWordChain Return Value | Explanation |
|---|---|---|
["an", "band", "band", "abandon"] |
true |
Each element contains the previous element as a substring. |
["to", "too", "stool", "tools"] |
false |
"tools" does not contain the substring "stool". |
Complete the isWordChain method:
Code Runner Challenge
Complete the isWordChain method. Return true if each element of wordList (except the first) contains the previous element as a substring, and false otherwise.
(b) Write the createList method
Write the createList method, which creates and returns an ArrayList<String>. The method identifies strings in wordList that start with target and returns a new ArrayList containing each identified string without the starting occurrence of target. Elements must appear in the returned list in the same order as they appear in wordList.
Consider an example where wordList contains the following strings:
["catch", "bobcat", "catchacat", "cat", "at"]
The following table shows the ArrayList returned by some calls to createList. In all cases, wordList is unchanged.
| Method Call | ArrayList Returned by createList | Explanation |
|---|---|---|
createList("cat") |
["ch", "chacat", ""] |
Only "catch", "catchacat", and "cat" begin with "cat". |
createList("catch") |
["", "acat"] |
Only "catch" and "catchacat" begin with "catch". |
createList("dog") |
[] |
None of the words in wordList begin with "dog". |
Write the createList method:
Code Runner Challenge
Complete the createList method. It should return an ArrayList containing strings from wordList that start with target, with the initial occurrence of target removed from each string.