Checker is #4 from the from the 2008 AP Computer Science A Free Response problems.
https://secure-media.collegeboard.org/apc/ap08_comp_sci_a_frq.pdf
Part (a) SubstringChecker class
public class SubstringChecker implements Checker
{
private String str;
public SubstringChecker(String str)
{
this.str = str;
}
public boolean accept(String text)
{
return text.indexOf(str) != -1;
}
}
See Class writing order for a technique to respond to AP CS FR that request an entire class.
Part (b) AndChecker class
public class AndChecker implements Checker
{
private Checker c1, c2;
public AndChecker(Checker c1, Checker c2)
{
this.c1 = c1;
this.c2 = c2;
}
public boolean accept(String text)
{
return c1.accept(text) && c2.accept(text);
}
}
Part (c) Assignment to yummyChecker
Checker notA = new NotChecker(aChecker);
Checker notK = new NotChecker(kChecker);
yummyChecker = new AndChecker(notA, notK);
A common mistake on this part is creating a single NotChecker.
The Checker created below accepts strings that do not contain "artichokes" or that do not contain "kale", which is not the requested behavior.
new NotChecker(new AndChecker(aChecker, kChecker))
2008 AP CS Exam Free Response Solutions
Help & comments
Get help from AP CS Tutor Brandon Horn