CodeWordChecker free response answer 2

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;
  }
}

2 thoughts on “CodeWordChecker free response answer

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

    possible code for other 1 param constructor

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

    • Reply Brandon Horn May 19,2018 12:05 pm

      You could call one constructor from the other but the syntax would be:

      public CodeWordChecker(String notIn)
      {
        this(6, 20, notIn);
      }
      

Leave a Reply