CodeWordChecker free response answer 1

CodeWordChecker free response problem from the 2018 AP Computer Science A Exam.

CodeWordChecker is #3 from the from the 2018 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/ap/pdf/ap18-frq-computer-science-a.pdf

CodeWordChecker class

public class CodeWordChecker implements StringChecker
{
  private int minLength;
  private int maxLength;
  private String notIn;

  public CodeWordChecker(int minLength, int maxLength, String notIn)
  {
    this.minLength = minLength;
    this.maxLength = maxLength;
    this.notIn = notIn;
  }

  public CodeWordChecker(String notIn)
  {
    this.minLength = 6;
    this.maxLength = 20;
    this.notIn = notIn;
  }

  public boolean isValid(String str)
  {
    return str.length() >= minLength
      && str.length() <= maxLength &&
      str.indexOf(notIn) == -1;
  }
}

One comment on “CodeWordChecker free response answer

  1. Mark Moylan May 18,2018 12:19 pm

    possible code for other 1 param constructor

    public codeWordChecker(String word){
    codeWordChecker(6,20,word);
    }

Comments are closed.