Phrase free response problem from the 2017 AP Computer Science A Exam.
Phrase is #3 from the from the 2017 AP Computer Science A Free Response problems.
https://secure-media.collegeboard.org/ap/pdf/ap-computer-science-a-frq-2017.pdf
Part (a) – replaceNthOccurrence method
public void replaceNthOccurrence(String str, int n, String repl)
{
int index = findNthOccurrence(str, n);
if(index != -1)
{
currentPhrase = currentPhrase.substring(0, index) + repl +
currentPhrase.substring(index + str.length());
}
}
Part (b) – findLastOccurrence method (considered solution)
This is my considered solution using the method from part (a). This solution has the advantage of not running findNthOccurrence an extra time. The solution I wrote under time constraints is farther down.
public int findLastOccurrence(String str)
{
int n = 1;
int index = -1;
int nextIndex = findNthOccurrence(str, n);
while(nextIndex != -1)
{
index = nextIndex;
n++;
nextIndex = findNthOccurrence(str, n);
}
return index;
}
Part (b) – findLastOccurrence method (solution outside Exam)
This solution uses the lastIndexOf method, which isn’t in the AP Java Subset. This is what I would write if I had to perform this task as part of an actual program.
To clarify, this solution is not acceptable on the Exam because the question requires the use of the method from part (a).
public int findLastOccurrence(String str)
{
return currentPhrase.lastIndexOf(str);
}
Part (b) – findLastOccurrence method (initial solution)
This was my solution under time constraints. I advise my students to avoid looking for solutions outside the subset on the Exam, so I avoid them as well.
public int findLastOccurrence(String str)
{
int n = 1;
while(findNthOccurrence(str, n) != -1)
n++;
if(n == 1)
return -1;
return findNthOccurrence(str, n - 1);
}
Last index of isnt allowed
True, but only because of the requirement that students use the method from part (a). Students are allowed to use any valid Java API methods, though I don’t encourage my students to look for solutions outside the subset during the Exam.
I clarified the header for my
lastIndexOf
solution and added an explicit note that it isn’t a valid solution on the Exam.I didn’t see that you had to use findNthOccurrence for findLastOccurrence. Instead I used a for loop going backwards. My program produces the right output but how much will I get docked off for not using findNthOccurrence.
You’ll almost certainly lose something. The scoring guide hasn’t been released yet so I can’t say how much.
Do you not have to consider edge cases in part a? What if the part after repl has an out of bounds error?
This is a great question. In general you do have to consider edge cases unless they are covered by a precondition. The College Board hasn’t released the scoring guides yet so we don’t know for sure in this case. My guess would be yes, you have to avoid going out of bounds.
My solution avoids this issue with careful use of both
substring
methods. You can ask for a substring of length 0 from the 2 parametersubstring
method and you can go 1 spot out of bounds (but not more) with the 1 parametersubstring
method."brandon".substring(0, 0)
will return an empty string."brandon".substring(7)
will return an empty string."brandon".substring(7, 8)
will cause a runtime error.What about searching for the string starting from the end of the phrase on findLastOccurrence?
The instructions specified that you must use the method from part (a). A solution that didn’t would almost certainly not receive full credit.
In case you want to run the methods of phase to see output, here is findNthOccurrence:
Note: formatting adjusted