LogMessage, the second free response question on the 2016 AP Computer Science exam, has you working with strings and ArrayLists.
For the constructor you are given a String parameter
message that is guaranteed to contain exactly one
: character. Everything to the left of the colon goes into an instance variable called
machineId and everything after goes into
Using the AP Java subset, this is an
indexOf problem. First you need to find where the colon is in
message and then use that number to
substring out everything before and after.
pos is the position of the colon.
The other option, if you’re willing to go a bit out of the Java subset, is to use the String
split method. It splits a string into an array of strings based on a regular expression. Since a colon isn’t part of regex, we can just put the colon in quotes.
Next you’re going to implement a method called
containsWord that returns
false if a keyword is found in
description matching a specific set of rules.
keywordis found in
- at the start of
descriptionand has a space after it
- at the end of
descriptionand has a space before it
- somewhere else in
descriptionwith a space before and after
- at the start of
For a solution that stays within the subset, we’re going to use
I broke this down to 4 cases, any of which should return
keywordare exactly the same
keywordfollowed by a space
keywordis somewhere in
descriptionwith spaces both before and after
keywordis at the end of
descriptionwith a space before it
The last case took a bit more work because it failed when the
description were the same length, but not equal. So my last condition checks both of those things.
This is a solution that one of my students shared with me.
Their code pads
description with spaces on both ends and looks for
keyword padded with spaces. This is one of those snippets of code that I really like to see from students because it shows they really understand the question and the methods needed to solve it.
This also could have been solved using regular expressions, although that would have been way out of the AP Java subset.
The short explanation is that this looks to see if
description matches anything, followed by either the start of the string or whitespace, followed by
keyword followed by whitespace or the end of the string, followed by anything.
Even if I was really comfortable with regex I still wouldn’t have written this solution during the actual AP exam. I’d be too worried that the grader might not be familiar with regex and would count it incorrect.
And last you’re going through a list of
LogMessage objects and removing those that contain a specific keyword.
I created a new
out that’s going to contain the
LogMessages that were removed.
I then went backwards through
messageList looking for
LogMessages that contained the
keyword. I like to go backwards when removing from a
List because it keeps the loop from skipping over elements after a removal.
And when it finds one to remove, it gets added at the beginning of
out. It has to be inserted at position zero because of the backwards loop. Otherwise
out would be backwards.
Want to stay in touch and keep up to date with the latest posts @ CompSci.rocks?