Sprint View

2018 FRQ Question 3

7 min read

Question Overview

This question involves creating a class that checks if strings meet specific criteria. You will implement the CodeWordChecker class that implements the provided StringChecker interface.

StringChecker Interface (Provided)

Code Runner Challenge

Implement CodeWordChecker

View IPYNB Source
public interface StringChecker {
    /** Returns true if str is valid. */
    boolean isValid(String str);
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

CodeWordChecker Specification

The CodeWordChecker class:

  • Implements the StringChecker interface
  • Instance Variables:
    • minLength - minimum acceptable length
    • maxLength - maximum acceptable length
    • notAllowed - a string that must NOT appear in valid strings
  • Constructor 1: Takes three parameters (minLen, maxLen, symbol) and initializes all instance variables
  • Constructor 2: Takes one parameter (symbol) and uses default values: minLength = 6, maxLength = 20
  • isValid(): Returns true if the string’s length is between min and max (inclusive) AND does not contain the notAllowed string

Key Insights

  • A string is valid if it meets ALL three criteria: length ≥ minLength, length ≤ maxLength, and does not contain notAllowed
  • Use indexOf() to check if a string contains another string (returns -1 if not found)
  • Constructor overloading allows for different ways to initialize the same class
// CODE_RUNNER: Implement CodeWordChecker

// StringChecker interface (provided)
interface StringChecker {
    boolean isValid(String str);
}

class CodeWordChecker implements StringChecker {
    private int minLength;
    private int maxLength;
    private String notAllowed;
    
    public CodeWordChecker(int minLen, int maxLen, String symbol) {
        minLength = minLen;
        maxLength = maxLen;
        notAllowed = symbol;
    }
    
    public CodeWordChecker(String symbol) {
        minLength = 6;
        maxLength = 20;
        notAllowed = symbol;
    }
    
    public boolean isValid(String str) {
        return str.length() >= minLength && 
               str.length() <= maxLength && 
               str.indexOf(notAllowed) == -1;
    }
}

// Test code (you can modify this to test your implementation)
public class Main {
    public static void main(String[] args) {
        System.out.println("Testing Example 1: 3-parameter constructor");
        StringChecker sc1 = new CodeWordChecker(5, 8, "$");
        System.out.println("conDUC" + " is valid: " + sc1.isValid("conDUC"));  // Expected: true
        System.out.println("conduct$" + " is valid: " + sc1.isValid("conduct$"));  // Expected: false
        System.out.println("con" + " is valid: " + sc1.isValid("con"));  // Expected: false
        
        System.out.println("Testing Example 2: 1-parameter constructor");
        StringChecker sc2 = new CodeWordChecker("pass");
        System.out.println("MyPass" + " is valid: " + sc2.isValid("MyPass"));  // Expected: true
        System.out.println("MyPassword" + " is valid: " + sc2.isValid("MyPassword"));  // Expected: false
        System.out.println("short" + " is valid: " + sc2.isValid("short"));  // Expected: false
    }
}
Main.main(null);

Example 1: Three-Parameter Constructor

This shows how to use the constructor with custom min/max lengths and a forbidden string.

Setup: CodeWordChecker sc1 = new CodeWordChecker(5, 8, "$");

  • Minimum length: 5
  • Maximum length: 8
  • Not allowed: “$”
Test String Length Contains “$”? Length Check Symbol Check Result
“conDUC” 6 No 5 ≤ 6 ≤ 8 ✓ No “$” ✓ true
“conduct$” 8 Yes 5 ≤ 8 ≤ 8 ✓ Has “$” ✗ false
“con” 3 No 3 < 5 ✗ No “$” ✓ false
“coNDUCTing” 10 No 10 > 8 ✗ No “$” ✓ false
”$” 1 Yes 1 < 5 ✗ Has “$” ✗ false
“dog$” 4 Yes 4 < 5 ✗ Has “$” ✗ false
“today!” 6 No 5 ≤ 6 ≤ 8 ✓ No “$” ✓ true

Key Observations:

  1. ALL conditions must be true for the string to be valid
  2. If the string fails any single check, the result is false
  3. The forbidden string can appear anywhere in the test string

Example 2: One-Parameter Constructor

This shows how the single-parameter constructor uses default values for min and max length.

Setup: CodeWordChecker sc2 = new CodeWordChecker("pass");

  • Minimum length: 6 (default)
  • Maximum length: 20 (default)
  • Not allowed: “pass”
Test String Length Contains “pass”? Length Check Symbol Check Result
“MyPass” 6 No 6 ≤ 6 ≤ 20 ✓ No “pass” ✓ true
“MyPassword” 10 Yes (“pass”) 6 ≤ 10 ≤ 20 ✓ Has “pass” ✗ false
“short” 5 No 5 < 6 ✗ No “pass” ✓ false
“verylongpasswordstring” 22 Yes (“pass”) 22 > 20 ✗ Has “pass” ✗ false
“passage” 7 Yes (“pass”) 6 ≤ 7 ≤ 20 ✓ Has “pass” ✗ false

Important Notes:

  1. The default constructor is a convenience - it calls the same logic with preset values
  2. String matching is case-sensitive: “pass” ≠ “Pass”
  3. The forbidden string can be a substring anywhere in the test string
  4. indexOf(notAllowed) == -1 means the forbidden string was NOT found

Submit Your Solution

Submission Counter

Attempts remaining: 3 / 3

Solution

Here is the complete implementation of the CodeWordChecker class that satisfies all points on the rubric.

Complete Code

public class CodeWordChecker implements StringChecker {
    private int minLength;
    private int maxLength;
    private String notAllowed;
    
    public CodeWordChecker(int minLen, int maxLen, String symbol) {
        minLength = minLen;
        maxLength = maxLen;
        notAllowed = symbol;
    }
    
    public CodeWordChecker(String symbol) {
        minLength = 6;
        maxLength = 20;
        notAllowed = symbol;
    }
    
    public boolean isValid(String str) {
        return str.length() >= minLength && 
               str.length() <= maxLength && 
               str.indexOf(notAllowed) == -1;
    }
}

Part-by-part Explanation

Class Declaration (1 point)

public class CodeWordChecker implements StringChecker
  • Must include the implements StringChecker clause
  • This makes it a requirement to implement the isValid() method

Instance Variables (1 point)

private int minLength;
private int maxLength;
private String notAllowed;
  • All three instance variables must be declared as private
  • minLength and maxLength are int types
  • notAllowed is a String

Three-Parameter Constructor (3 points)

public CodeWordChecker(int minLen, int maxLen, String symbol) {
    minLength = minLen;
    maxLength = maxLen;
    notAllowed = symbol;
}
  • 1 point: Correct header with three parameters
  • 1 point: Initializes minLength using first parameter
  • 1 point: Initializes maxLength and notAllowed using remaining parameters

One-Parameter Constructor (1 point)

public CodeWordChecker(String symbol) {
    minLength = 6;
    maxLength = 20;
    notAllowed = symbol;
}
  • Uses the parameter to set notAllowed
  • Uses default literal values 6 and 20 for min and max
  • Alternative: Could call this(6, 20, symbol);

isValid() Method (4 points)

public boolean isValid(String str) {
    return str.length() >= minLength && 
           str.length() <= maxLength && 
           str.indexOf(notAllowed) == -1;
}
  • 1 point: Correct header: public boolean isValid(String __)
  • 1 point: Checks if length is between min and max (inclusive)
  • 1 point: Checks if string does NOT contain notAllowed using indexOf()
  • 1 point: Returns true only if ALL conditions are met, false otherwise

How It Works

  1. The class implements an interface, so it must provide the isValid() method.
  2. Two constructors provide flexibility: one with custom values, one with defaults.
  3. The isValid() method performs three checks using logical AND (&&) to ensure all conditions are met.
  4. The indexOf() method returns -1 if the substring is not found, which means the string passes that check.

Scoring Guidelines

Points Breakdown (10 points total)

Class Declaration and Instance Variables (2 points)

  • 1 point: Declares header: public class CodeWordChecker implements StringChecker
  • 1 point: Declares all appropriate private instance variables (minLength, maxLength, notAllowed)

Constructors (4 points)

  • 1 point: Declares headers for both constructors:
    • public CodeWordChecker(int __, int __, String __)
    • public CodeWordChecker(String __)
  • 1 point: Uses all three parameters to initialize instance variables in 3-parameter constructor
  • 1 point: Initializes minLength using first parameter in 3-parameter constructor
  • 1 point: Uses parameter and default values (6 and 20) to initialize instance variables in 1-parameter constructor

isValid() Method (4 points)

  • 1 point: Declares header: public boolean isValid(String __)
  • 1 point: Checks for length between min and max inclusive (uses >= and <=)
  • 1 point: Checks that the string does not contain the unwanted string (uses indexOf() or contains())
  • 1 point: Returns true if length is between min and max AND does not contain the unwanted string, false otherwise

Common Mistakes to Avoid

  • Forgetting to include implements StringChecker in the class header
  • Using == instead of .equals() when comparing strings
  • Using str.indexOf(notAllowed) == 0 (only checks if it starts with the unwanted string)
  • Using > or < instead of >= and <= for length checks (the bounds are inclusive)
  • Forgetting to make instance variables private
  • Not initializing all instance variables in the constructors
  • Using wrong default values in the 1-parameter constructor (must be 6 and 20)
  • Using OR (||) instead of AND (&&) in the isValid() method
  • Returning true when the unwanted string IS found (logic should be inverted)

Alternative Approaches

Using constructor chaining:

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

Using contains() instead of indexOf():

public boolean isValid(String str) {
    return str.length() >= minLength && 
           str.length() <= maxLength && 
           !str.contains(notAllowed);
}

Both of these alternatives are equally valid and will receive full credit.

Course Timeline