2020 AP CSA Sample Q2 - CheckDigit (Methods & Validation)

Topic: Integer manipulation, validation logic, and using helper methods

Skills Tested: Integer division, modulo operator, method calls, boolean logic

Curriculum Alignment: Unit 1 (Primitive Types), Unit 2 (Objects)

Allotted Time: 15 minutes (plus 5 minutes to submit)

¹ Note: This is a sample question from the 2020 at-home exam format. Part (b) asks for a written description rather than code.

Question

This question involves the use of check digits, which can be used to help detect if an error has occurred when a number is entered or transmitted electronically.

Provided CheckDigit Class

public class CheckDigit
{
    /** Returns the check digit for num
     *  Precondition: The number of digits in num is between one and six, inclusive.
     *                num >= 0
     */
    public static int getCheck(int num)
    { /* implementation not shown */ }

    /** Returns true if numWithCheckDigit is valid, or false otherwise,
     *  as described in part (a)
     *  Precondition: The number of digits in numWithCheckDigit is
     *                between two and seven, inclusive.
     *                numWithCheckDigit >= 0
     */
    public static boolean isValid(int numWithCheckDigit)
    { /* to be implemented in part (a) */ }

    // There may be variables and methods not shown.
}

Part (a): Write the isValid Method

Write the isValid method. The method returns true if its parameter numWithCheckDigit, which represents a number containing a check digit, is valid, and false otherwise.

The check digit is always the rightmost digit of numWithCheckDigit.

Examples

Method Call Return Value Explanation
getCheck(159) 2 The check digit for 159 is 2
isValid(1592) true 159 with check digit 2 is valid
isValid(1593) false 159 with check digit 3 is NOT valid (should be 2)

Part (b): Design Discussion

A programmer wants to modify the CheckDigit class to keep track of how many times a call to isValid is made with an incorrect check digit. Any time a call to isValid is made with an incorrect check digit, the count should be increased by one.

The programmer would like to implement this change without making any changes to the signature of the isValid method or overloading isValid.

Write a description of how you would change the CheckDigit class to support this modification. (No code required)

Part (a) Solution: isValid

✔ Try It Yourself First! Attempt to write your solution before viewing the answer below.

Solution Code

public static boolean isValid(int numWithCheckDigit)
{
    // Extract the check digit (rightmost digit)
    int checkDigit = numWithCheckDigit % 10;
    
    // Extract the number without the check digit
    int num = numWithCheckDigit / 10;
    
    // Compare the extracted check digit with the calculated one
    return checkDigit == getCheck(num);
}

Step-by-Step Breakdown

For numWithCheckDigit = 1592:

  1. Extract check digit: 1592 % 10 = 2
  2. Extract original number: 1592 / 10 = 159 (integer division)
  3. Calculate expected check digit: getCheck(159) = 2
  4. Compare: 2 == 2true

For numWithCheckDigit = 1593:

  1. Extract check digit: 1593 % 10 = 3
  2. Extract original number: 1593 / 10 = 159
  3. Calculate expected check digit: getCheck(159) = 2
  4. Compare: 3 == 2false
¡ Key Concepts - Digit Extraction:
  • Modulo operator (%): num % 10 extracts the last digit
  • Integer division (/): num / 10 removes the last digit
¡ Practice Tip: This pattern of extracting digits using % 10 and / 10 appears frequently on AP CSA exams. Make sure you're comfortable with it!

Part (b): Design Discussion

This part asks you to describe (not code) how to track invalid check digit attempts.

Sample Response:

To track how many times isValid is called with an incorrect check digit, I would make the following changes to the CheckDigit class:

  • Add a static instance variable: private static int invalidCount = 0; - This variable stores the count of invalid check digit attempts. It is static because we want to track across all calls, not per instance.
  • Modify the isValid method: Before returning false (when the check digit is incorrect), increment the counter: invalidCount++;
  • Add an accessor method: public static int getInvalidCount() - This method returns the current value of invalidCount so other code can access the count.

This approach requires no changes to the method signature of isValid since we're only adding code inside the method and adding new class members.

Common Mistakes to Avoid

Part (a) Common Errors:

  • Extracting wrong digit: Using / 10 when you need % 10 for the check digit
  • Order confusion: Mixing up which value is the number and which is the check digit
  • Not using getCheck: Trying to calculate the check digit yourself instead of calling the helper
  • Wrong comparison: Using .equals() for primitive int comparison (use ==)
  • Forgetting to return: Doing the comparison but not returning the result

Part (b) Common Errors:

  • Suggesting changes to the method signature: The question says not to do this
  • Using an instance variable instead of static: Would reset for each object
  • Not describing how to access the count: Forgetting the getter method

Why This Pattern Matters

Check digits are used in real-world applications like:

  • Credit card numbers: The Luhn algorithm validates card numbers
  • ISBN numbers: Book identification codes include a check digit
  • UPC barcodes: Product barcodes use check digits
  • Routing numbers: Bank routing numbers are validated this way

Understanding how to extract digits using % and / is a fundamental skill in AP CSA!

Official College Board Resources

Last updated:

Contact form