FRQ 2020 - Methods and Control Structures
Answering Methods and Control Structures on the 2020 AP test.
Allotted time: 15 minutes (plus 5 minutes to submit) 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. The CheckDigit class is shown below. You will write one method of the 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.
}
(a) 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. The following table shows some examples of the use of isValid.
Method Call | Return Value | Explanation |
---|---|---|
getCheck(159) | 2 | The check digit for 159 is 2. |
isValid(1592) | True | The number 1592 is a valid combination of a number (159) check digit (2). |
isValid(1593) | False | The number 1593 is not a valid combination of a number (159) check digit (3) because 2 is the check digit for 159. |
Complete method isValid below. You must use getCheck appropriately to receive full credit.
/** 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)
public static boolean isValid(int numWithCheckDigit) {
// Takes the remainder of dividing by 10 to get the last digit.
int lastDigit = numWithCheckDigit % 10;
// Divides by 10 to get all the digits in front of the last one.
int num = numWithCheckDigit / 10;
// Checks if get check returns the same value as the last digit.
if(getCheck(num) == lastDigit) {
return true;
}
else {
return false;
}
}
(b) 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 in order to support this modification. Do not write the program code for this change. Make sure to include the following in your response.
- Identify any new or modified variables or methods.
- Describe, for each new or revised variable or method, how it would change or be implemented, including visibility and type.
Part B Answer:
- To count how many times the isValid method returns false, a new integer counter field would need to be created for the checkDigit class. Every time, either in the else statement, in the isValid method, or whenever the isValid method returns false within another class or method, this field should be increased by 1. If this field will be accessed within another class, it should be a public field rather than a private field.