APCSAUnit 4 FRQs
APCSAUnit 4 FRQs
Unit 4 FRQ
1. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
2. This question involves analyzing integer values that are obtained using the getInt method in the following
IntegerAnalysis class. You will write one method in the class.
/** Analyzes n values obtained from the getInt method and returns the
proportion
* of these values that meet the criteria described in part (a)
* Precondition
Precondition: max > 0, n > 0
*/
public static double analyzeInts(int max, int n)
{ /* to be implemented in part (a) */ }
(a) Write method analyzeInts, which obtains n values using the getInt method and returns the
proportion of the obtained values that meet all the following criteria.
For example, if max is 10 and the values obtained by getInt are 6, -3, 5, 0, 12, 3, 3, and 9,
then analyzeInts should return 0.5 because four of the eight values (6, 3, 3, and 9) meet all the
criteria.
Unit 4 FRQ
/** Analyzes n values obtained from the getInt method and returns the
proportion of
* these values that meet the criteria described in part (a)
* Precondition
Precondition: max > 0, n > 0
*/
public static double analyzeInts(int max, int n)
(b) A programmer wants to modify the IntegerAnalysis class so that in the analyzeInts method, the
check for divisibility by an integer can vary between method calls. For example, in one call to
analyzeInts, the method might check for divisibility by 3, and in another call to analyzeInts, the
method might check for divisibility by 10. The programmer wants to implement this change without making
any changes to the signature of the analyzeInts method or overloading analyzeInts.
Write a description of how you would change the IntegerAnalysis class in order to support this
modification. Do not write the program code for this change.
Part A – analyzeInts
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they inappropriately return inside the loop
Unit 4 FRQ
+1 [Skill 3.C] Identifies values to include in the count based on criteria and parameters
Responses can still earn the point even if they identify values to include that are not returned by or are returned by
an incorrect call to
Responses can still earn the point even if they accumulate count incorrectly
Responses will not earn the point if they calculate the proportion incorrectly based on count and
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 && num
0 1 2 3 4 5
Unit 4 FRQ
Canonical Solution:
0 && num < max && (num % 3 == 0)) Line 8: { Line 9: count++; Line 10: } Line 11: } Line 12: return count / n; Line
13: } end code">
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
o describe a local variable that is assigned a value only once during the method call, where the value is generated
randomly or from user input
Responses will not earn the point if they describe a change to the method header
· method would need to change so that instead of checking for divisibility by 3 using %3, it would use the
new variable
Responses can still earn the point even if they describe a change to the header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
2. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
2. This question involves adding apples to a bag until the weight of the bag is within a specified range. The weight
of each apple is obtained using the getApple method in the following AppleBagger class. You will write
one method in the class.
/** Returns the number of apples that are added to a bag, as described
in part (a)
* Precondition
Precondition: 0 < allowedVariation < targetWeight
*/
public static int bagApples(double targetWeight,
double allowedVariation)
{ /* to be implemented in part (a) */ }
(a) Write the method bagApples, which obtains the weights of apples to be added to a bag using calls to the
getApple method and returns the number of apples that are added to the bag until the combined weight exceeds
targetWeight minus allowedVariation.
For example, if targetWeight is 10.0 and allowedVariation is 0.5, the bagApples method
will return the number of apples that are added until the combined weight of the apples exceeds 9.5.
Unit 4 FRQ
/** Returns the number of apples that are added to a bag, as described in
part (a)
* Precondition
Precondition: 0 < allowedVariation < targetWeight
*/
public static int bagApples(double targetWeight,
double allowedVariation)
(b) A programmer wants to modify the AppleBagger class so that the allowed variation in the bagApples
method is always equal to percent of the weight of the first apple added to the bag during a call to
bagApples, rather than a parameter passed to the method.
Write a description of how you would change the AppleBagger class in order to support this modification. Do
not write the program code for this change.
Part A – bagApples
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· confuse with
Responses will not earn the point if they use the incorrect direction for the comparison
Unit 4 FRQ
· use or in comparison
+1 [Skill 3.C] Maintains total weight of apples added so far (in the context of a loop)
· call incorrectly
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· parameter is removed
· after retrieving the first apple, a variable would be set based on that weight, and this variable would be used in the loop
test in place of the removed parameter
Responses can still earn the point even if they do not specify a change to the method header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
3. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves analyzing a salesperson’s sales. Sales amounts are obtained using the getSales
method in the following SalesSimulator class. You will write one method in the class.
/** Analyzes sales for numDays days obtained from the getSales method
and
* returns the total bonus earned by the salesperson, in dollars, as
described in part (a)
* Precondition
Precondition: goal > 0, numDays > 0
*/
public static int calculateBonus(int goal, int numDays)
{ /* to be implemented in part (a) */ }
(a) Write the SalesSimulator method calculateBonus, which obtains the daily sales of a salesperson
and applies a bonus based on how close to meeting or exceeding the daily sales goal the salesperson came. It
obtains daily sales for numDays days using the getSales method. For each day, if the daily sales are at
least percent of goal but less than goal, the salesperson receives a bonus. The salesperson receives a
bonus if the daily sales are greater than or equal to goal. Method calculateBonus returns the total
bonus received, in dollars, over all days.
The following table shows the bonuses received as a result of the method call
Unit 4 FRQ
SalesSimulator.calculateBonus(200, 3).
getSales()
Daily
Day Explanation
Bonus
Return Value
For the sales shown in the table above, SalesSimulator.calculateBonus(200, 3) should return the
total bonus 125.
/** Analyzes sales for numDays days obtained from the getSales method and
returns
* the total bonus earned by the salesperson, in dollars, as described
in part (a)
* Precondition
Precondition: goal > 0, numDays > 0
*/
public static int calculateBonus(int goal, int numDays)
(b) A programmer wants to modify the SalesSimulator class so that the daily goal is maintained in a
variable with a value that cannot be changed once it is initialized.
Write a description of how you would change the SalesSimulator class in order to support this
modification. Do not write the program code for this change.
Part A – calculateBonus
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Unit 4 FRQ
Responses can still earn the point even if they return early inside the loop
· classify a sales amount into the wrong category, or into both categories
+1 [Skill 3.C] Declares, initializes, and increments bonus appropriately in all cases (algorithm)
Responses can still earn the point even if they classify a sales amount into the wrong category, or into both categories
Responses can still earn the point even if they determine bonus incorrectly
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
= goal) Line 8: { Line 9: bonus += 75; Line 10: } Line 11: else if (sales >= 0.8 * goal) Line 12: { Line 13: bonus += 50;
Line 14: } Line 15 is blank. Line 16: } Line 17: return bonus; Line 18: } end code">
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
= goal) Line 8: { Line 9: bonus += 75; Line 10: } Line 11: else if (sales >= 0.8 * goal) Line 12: { Line 13: bonus += 50;
Line 14: } Line 15 is blank. Line 16: } Line 17: return bonus; Line 18: } end code">
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
· or "constant"
· method would need to change so that it only has a single parameter and would use the
variable to determine if the goal was met
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
4. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
2. This question involves manipulating strings that are made up of uppercase letters. The Puzzle class contains
methods used to modify keys. You will write one method in this class. A partial declaration of the Puzzle class
is shown below.
(a) Write the Puzzle method changeKey, which uses the parameter letter to return a string based on
the parameter key. The parameter letter contains a single uppercase letter. The parameter key has at
least two letters and all letters in key are uppercase and unique. A sample key is "ABC".
The following describes the string that should be returned by the method.
If the letter is contained in the key, the method should return a new string containing all letters in the key
except letter, in their original order.
If the letter does not appear in the key, the method should return the letter.
Unit 4 FRQ
(b) The programmer would like to modify the Puzzle class to keep track of how many times a call to
changeKey results in a change to a key. Any time a call to changeKey results in a change to the key, the
count of key modifications should be increased by one. The programmer would like to implement this change
without making any changes to the signature of the changeKey method or overloading changeKey.
Write a description of how you would change the Puzzle class in order to support this modification. Do not
write the program code for this change.
Part A – changeKey
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they incorrectly determine whether letter is in
Unit 4 FRQ
Responses can still earn the point even if they do not include all characters other than in the constructed string
Responses will not earn the point if they do not create a new
+1 [Skill 3.A] Adds preceding and following substring of the identified position in to the constructed
· identify the wrong position or include the identified position when adding characters to the constructed string
Responses will not earn the point if they do not identify a position
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
Responses can still earn the point even if they specify the variable as an instance variable
Responses will not earn the point if they describe a change to the method header
Unit 4 FRQ
Responses can still earn the point even if they describe a change to the method header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in
that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not
receive full credit.
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. An algorithm for computing a check digit, based on the digits of a number, is provided in
part (a).
The CheckDigit class is shown below. You will write two methods of the CheckDigit class.
Unit 4 FRQ
Unit 4 FRQ
5. (a) Write the getCheck method, which computes the check digit for a number according to the following rules.
Multiply the first digit by 7, the second digit (if one exists) by 6, the third digit (if one exists) by 5, and so on.
The length of the method's int parameter is at most six; therefore, the last digit of a six-digit number will be
multiplied by 2.
Add the products calculated in the previous step.
Extract the check digit, which is the rightmost digit of the sum calculated in the previous step.
Complete the getCheck method below. You must use getNumberOfDigits and getDigit appropriately
to receive full credit.
/** Returns the check digit for num, as described in part (a).
* Precondition: The number of digits in num is between one and six,
inclusive.
* num >= 0
*/
public static int getCheck(int num)
(b) 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
Unit 4 FRQ
digit of numWithCheckDigit.
Return
Method Call Explanation
Value
getCheck(159) 2 The check digit for 159 is 2.
The number 1592 is a valid combination of a
isValid(1592) true
number (159) and its check digit (2).
The number 1593 is not a valid combination of a
isValid(1593) false number (159) and its check digit (3) because 2 is the check
digit for 159.
Complete method isValid below. Assume that getCheck works as specified, regardless of what you wrote in
part (a). You must use getCheck appropriately to receive full credit.
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.C] Calculates all partial sums (in the context of a loop, no bounds errors).
+1 [Skill 3.C] Returns the last digit of the calculated sum as the check digit.
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4
Canonical Solution:
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Unit 4 FRQ
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Unit 4 FRQ
6. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves guessing strings that may or may not appear in a secret phrase. Guesses are obtained
using the getGuess method in the following StringGuess class. You will write one method in the class.
(a) Write the StringGuess method checkGuesses, which obtains num guesses using the getGuess
method. Method checkGuesses returns the proportion of obtained guesses that appear in phrase.
Unit 4 FRQ
in part (a)
* Precondition
Precondition: num > 0, phrase.length() > 0
*/
public static double checkGuesses(String phrase, int num)
(b) A programmer wants to modify the StringGuess class so that the number of guesses analyzed by the
checkGuesses method is a random value between 1 and 100, inclusive, and is generated in the
StringGuess class. For example, in one call to checkGuesses, the number of guesses to analyze might
be 12, and in another call to checkGuesses, the number of guesses to analyze might be 77. Each value
between 1 and 100 must have an equal chance of being used as the number of guesses.
Write a description of how you would change the StringGuess class in order to support this modification. Do
not write the program code for this change.
Part A – checkGuesses
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they inappropriately return inside the loop
Responses can still earn the point even if they call incorrectly
· call or incorrectly
+1 [Skill 3.C] Updates count under appropriate conditions (in the context of a loop)
Responses can still earn the point even if they use incorrect logic to guard the update
Unit 4 FRQ
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
= 0) Line 10: { Line 11: count++; Line 12: } Line 13: } Line 14: return (double) count / num; Line 15: } end code">
0 1 2 3 4 5
Unit 4 FRQ
Canonical Solution:
= 0) Line 10: { Line 11: count++; Line 12: } Line 13: } Line 14: return (double) count / num; Line 15: } end code">
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· would change to calculate a random number between 1 and 100 before the loop, and use this value in
the loop condition in place of the parameter
Responses can still earn the point even if they do not specify a change to the header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
7. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
6. This question involves computing an integer based on two input strings. You will write the compute method
of the following Operator class.
(a) Write the compute method of the Operator class. The method calculates an integer value based on an
input string with even length. The first half and the second half of input are transformed into numeric values
by calls to transform. The numeric values are then used in a calculation based on the mathematical operation
represented by the string, op. The method returns the calculated result.
The helper method transform has been provided. The method returns an integer value based on its String
parameter. For example, the method call transform("pear") might return the int value 3.
The mathematical operations represented by op are described in the following table. Assume that value1 is
the numeric value obtained by calling transform on the first half of input and value2 is the numeric
value obtained by calling transform on the second half of input.
Unit 4 FRQ
For example, assume that calls to transform are made from the Operator class and that
transform("orang") returns 2 and transform("epear") returns 6. Then the method call
Operator.compute("orangepear", "$$") should return 8.
As another example, the method call Operator.compute("orangepear", "##") should return -4.
Complete method compute. You must use transform appropriately to receive full credit.
(b) A programmer wants to modify the Operator class so that the compute method has a single String
parameter containing both input and op.
Write a description of how you would change the Operator class in order to support this modification. Do not
write the program code for this change.
Part A – compute
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.A] Divides the input string into two substrings of equal length
Unit 4 FRQ
· call incorrectly
+1 [Skill 3.A] Compares to 3 possible values (in the context of a conditional statement)
Responses will not earn the point if they compare to possible operators using
+1 [Skill 3.C] Performs the calculation based on the value of and return values
Responses will not earn the point if they pair an incorrect mathematical operator with any of the 3 values of
+1 [Skill 3.C] Calculates and returns appropriate value in all cases (algorithm point)
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
+1 Divides the input string into two substrings of equal length (Points earned)
+1 Calls on two substrings of input string (Points earned)
+1 Compares to 3 possible values (in the context of a conditional statement) (Points earned)
+1 Performs the calculation based on the value of and return values (Points earned)
+1 Calculates and returns appropriate value in all cases (algorithm point) (Points earned)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
8. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves analyzing numbers that are obtained using the getNumber method in the following
NumberChecker class. You will write one method in the class.
(a) Write method countNumbers, which obtains values using the getNumber method and returns the
number of calls to getNumber that are made until n values are obtained that meet both of the following
criteria.
If max calls are made to getNumber without obtaining n values that meet the criteria, -1 is returned.
Unit 4 FRQ
A helper method, isTarget, has been provided. The method returns true if its integer parameter is a target
number and returns false otherwise. You must use isTarget appropriately to receive full credit.
(b) A programmer wants to modify the NumberChecker class so that in the countNumbers method, the
check for divisibility by an integer can vary between method calls. For example, in one call to
countNumbers, the method might check for divisibility by , and in another call to countNumbers, the
method might check for divisibility by .
The programmer would like to implement this change without making any changes to the signature of the
countNumbers method or overloading countNumbers.
Write a description of how you would change the NumberChecker class in order to support this modification.
Do not write the program code for this change.
Part A – countNumbers
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.C] Iterates until values that meet criteria are found or maximum number of input values are examined
Responses will not earn the point if they do not attempt to get the next number inside the loop
Responses can still earn the point even if they do not use a loop
Unit 4 FRQ
· call incorrectly
Responses can still earn the point even if they terminate the loop under incorrect conditions
· call incorrectly
· do not increment the counter in the context of a comparison of and a call to within a loop
+1 [Skill 3.C] Returns number of input values examined until values found that meet criteria, or otherwise
Responses can still earn the point even if they accumulate counts incorrectly
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
Unit 4 FRQ
0 1 2 3 4 5
+1 Iterates until values that meet criteria are found or maximum number of input values are examined
(Points earned)
+1 Calls (in the context of a condition or loop) (Points earned)
+1 Counts all numbers (Points earned)
+1 Counts the numbers that meet criteria (Points earned)
+1 Returns number of input values examined until values found that meet criteria, or otherwise
(Points earned)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
o describe a local variable that is assigned a value only once during the method call, where the value is generated
randomly or from user input
Responses will not earn the point if they describe a change to the header
· would change so that it uses the new variable instead of always using
Responses can still earn the point even if they describe a change to the method header
Responses will not earn the point if the same divisor is not used for all numbers
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
Unit 4 FRQ
0 1 2
Unit 4 FRQ
9. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves analyzing strings. You will write the countRepeat method of the following
StringAnalyzer class.
(a) The countRepeat method is used to count and return the number of times the string parameter
smallStr appears in the string parameter largeStr. In the case of overlapping occurrences of
smallStr, only the first occurrence is included in the overall count, as shown in the second and third calls to
countRepeat in the following table of examples.
Unit 4 FRQ
(b) A programmer would like to modify the StringAnalyzer class to limit the length of the large string that
can be passed as input to countRepeat. The limit could vary between calls to countRepeat. A call to
countRepeat with a value of largeStr that is longer than the maximum length would return –1. The
programmer would like to implement this change without making any changes to the signature of the
countRepeat method or overloading countRepeat.
Write a description of how you would change the StringAnalyzer class in order to support this
modification. Do not write the program code for this change.
Part A – countRepeat
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they calculate the length of using an incorrect call to
Responses will not earn the point if they do not use the length of to compare substrings of equal length
+1 [Skill 3.A] Uses and a substring of to guard the increment of the counter and update of the loop
control variable
Unit 4 FRQ
+1 [Skill 3.C] Updates loop control variable and counter based on result of comparison (algorithm)
Responses can still earn the point even if they calculate the length of using an incorrect call to
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Unit 4 FRQ
Canonical Solution:
Unit 4 FRQ
Part B – StringAnalyzer
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
o do not describe a variable or identify a type, but describe an appropriate identified value as generated only once during
the method call, randomly or from user input
Responses will not earn the point if they describe a change to the method header
· method would need to change so that it checks if the length of is less than the static variable
and returns if so
Responses can still earn the point even if they describe a change to the method header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
10. This question involves reasoning about a simulation of a frog hopping in a straight line. The frog attempts to hop
to a goal within a specified number of hops. The simulation is encapsulated in the following FrogSimulation class.
You will write two of the methods in this class.
a. Write the simulate method, which simulates the frog attempting to hop in a straight line to a goal from
the frog's starting position of 0 within a maximum number of hops. The method returns true if the frog
successfully reached the goal within the maximum number of hops; otherwise, the method returns false.
The FrogSimulation class provides a method called hopDistance that returns an integer representing the
distance (positive or negative) to be moved when the frog hops. A positive distance represents a move
toward the goal. A negative distance represents a move away from the goal. The returned distance may
vary from call to call. Each time the frog hops, its position is adjusted by the value returned by a call to
the hopDistance method.
The frog hops until one of the following conditions becomes true:
• The frog has taken the maximum number of hops without reaching the goal.
Unit 4 FRQ
The following example shows a declaration of a FrogSimulation object for which the goal distance is 24
inches and the maximum number of hops is 5. The table shows some possible outcomes of calling the
simulate method.
Complete method simulate below. You must use hopDistance appropriately to receive full credit.
/** Simulates a frog attempting to reach the goal as described in part (a). * Returns true if the frog
successfully reached or passed the goal during the simulation; * false otherwise. */
b. Write the runSimulations method, which performs a given number of simulations and returns the
proportion of simulations in which the frog successfully reached or passed the goal. For example, if the
parameter passed to runSimulations is 400, and 100 of the 400 simulate method calls returned true, then
the runSimulations method should return 0.25.
Unit 4 FRQ
Complete method runSimulations below. Assume that simulate works as specified, regardless of what
you wrote in part (a). You must use simulate appropriately to receive full credit.
Part A
1 point is earned for: Calls hopDistance and uses returned distance to adjust (or represent) the frog’s position
1 point is earned for: Initializes and accumulates the frog’s position at most maxHops times (must be in context of a
loop)
1 point is earned for: Determines if a distance representing multiple hops is at least goalDistance
1 point is earned for: Determines if a distance representing multiple hops is less than starting position
1 point is earned for: Returns true if goal ever reached, false if goal never reached or position ever less than starting
position
int position = 0;
position += hopDistance();
return true;
return false;
Unit 4 FRQ
return false;
0 1 2 3 4 5
1 point is earned for: Calls hopDistance and uses returned distance to adjust (or represent) the frog’s position
1 point is earned for: Initializes and accumulates the frog’s position at most maxHops times (must be in context of a
loop)
1 point is earned for: Determines if a distance representing multiple hops is at least goalDistance
1 point is earned for: Determines if a distance representing multiple hops is less than starting position
1 point is earned for: Returns true if goal ever reached, false if goal never reached or position ever less than starting
position
int position = 0;
position += hopDistance();
return true;
Unit 4 FRQ
return false;
return false;
Part B
1 point is earned for: Calls simulate the specified number of times (no bounds errors)
1 point is earned for: Calculates proportion of successful simulations using double arithmetic
int countSuccess = 0;
if(simulate())
countSuccess++;
0 1 2 3 4
Unit 4 FRQ
1 point is earned for: Calls simulate the specified number of times (no bounds errors)
1 point is earned for: Calculates proportion of successful simulations using double arithmetic
int countSuccess = 0;
if(simulate())
countSuccess++;
Unit 4 FRQ
11. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
6. This question involves translating a string consisting of a single uppercase letter based on a substitution rule.
The class Secret contains methods used to translate letters. You will write one method in this class. A partial
declaration of the Secret class is shown below.
(a) Write the Secret method newLetter, which is used to translate a string consisting of a single
uppercase letter based on a substitution rule. The parameter letter is the one-letter string to be translated. The
parameter rule is the substitution rule.
A substitution rule is represented by a string of at least two letters. All the letters in the substitution rule are
uppercase and unique. A sample substitution rule is "ABC".
• If the letter is contained in the rule, the method returns the next letter in the rule. For example, if the
rule is "CBA", "C" would be translated to "B", and "B" would be translated to "A". If the
letter appears at the end of the rule, the first letter of the rule is returned. Using the rule "CBA",
"A" would be translated to "C".
Unit 4 FRQ
• If the letter does not appear in the rule, it is returned. For example, "F" would be returned using the
rule "CBA" because "F" does not appear in the rule.
(b) The programmer would like to modify the Secret class so that the substitution rule is stored in the class
instead of passed as a parameter. The modification would need to support the ability for a calling program to
change the rule.
Write a description of how you would change the Secret class in order to support this modification. Do not
write the program code for this change.
Part A – newLetter
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Unit 4 FRQ
Responses can still earn the point even if they incorrectly determine whether is in
Responses can still earn the point even if they are off by one in calculating the last position
· identify a instead of a
· return a instead of a
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
-1 (t) Accesses single characters incorrectly, including calling incorrectly or using array or syntax
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "string"
Responses can still earn the point even if they specify the variable as an instance variable
Unit 4 FRQ
· would change to have only a single parameter (the letter to translate) and would access the rule
using the new variable
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
12. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
6. The MysteryNumber class contains methods used to analyze properties of numbers. You will write one
method of the MysteryNumber class.
(a) Write the static method nthMysteryNumber, which returns the mystery number, starting with . A
helper method, isMystery, has been provided. The method returns true if its parameter is a mystery
number and returns false otherwise.
Assume that the following numbers are the first mystery numbers and that there are more mystery numbers than
the ones shown below.
, , , , , ,
Unit 4 FRQ
Complete method nthMysteryNumber. You must use isMystery appropriately to receive full credit.
/** Returns the nth mystery number starting with 1, as described in part
(a)
* Precondition
Precondition: 1 <= n
* There are at least n mystery numbers between 1 and
* Integer.MAX_VALUE.
*/
public static int nthMysteryNumber(int n)
(b) A programmer wants to modify the MysteryNumber class so that the nthMysteryNumber method
returns the nth mystery number that is greater than or equal to a given integer value. For example, in one call to
nthMysteryNumber, the method might return the third mystery number that is greater than or equal to , and
in another call to nthMysteryNumber, the method might return the fifth mystery number that is greater than
or equal to .
Write a description of how you would change the MysteryNumber class in order to support this modification.
Do not write the program code for this change.
Part A – nthMysteryNumber
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.C] Loops through all numbers between and mystery number, inclusive
Responses can still earn the point even if they return early inside the loop
+1 [Skill 3.C] Increments count of mystery numbers based on comparison of return value and
Responses can still earn the point even if they call incorrectly
Unit 4 FRQ
+1 [Skill 3.C] Increments argument used in call to (in the context of a loop)
Responses can still earn the point even if they call incorrectly
Responses will not earn the point if they do not increment the argument in all cases
Responses can still earn the point even if they do not increment the argument to in all cases
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
+1 Loops through all numbers between and mystery number, inclusive (Points earned)
+1 Calls using an (Points earned)
+1 Increments count of mystery numbers based on comparison of return value and
(Points earned)
+1 Increments argument used in call to (in the context of a loop) (Points earned)
+1 Identifies and returns the mystery number (Points earned)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they describe a new variable in the class that is not explicitly stated as local to
the method
· the method begins counting mystery numbers starting at the given parameter instead of starting at 1
Responses can still earn the point even if they do not specify a change to the method header
Unit 4 FRQ
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
13. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves simulating the behavior of a dog, based on boolean values obtained by the
getResponse method in the following DogAnalysis class. You will write one method in the class.
/** Returns the number of times the dog was asked to sit, as described
in part (a)
* Precondition
Precondition: n >= 1
*/
public static int numUntilObedient(int n)
{ /* to be implemented in part (a) */ }
(a) Write the method numUntilObedient, which simulates repeatedly asking the dog to sit until the dog sits
on command n times in a row. The numUntilObedient method returns the number of times the dog was
commanded to sit before the dog obeyed the command n times in a row.
The helper method getResponse simulates the dog’s response to the sit command. It returns true if the
dog sat on command and returns false otherwise.
For example, if n is 3 and the values returned from getResponse are true, true, false, true,
false, true, true, and true, the numUntilObedient method should return 8 since the dog
obeyed the command on the , , and calls to getResponse.
Unit 4 FRQ
/** Returns the number of times the dog was asked to sit, as described in
part (a)
* Precondition
Precondition: n >= 1
*/
public static int numUntilObedient(int n)
(b) A programmer wants to modify the DogAnalysis class so that the numUntilObedient method is
given a value that represents the maximum number of times the dog should be commanded to sit. The method
would return true if the dog sat n times in a row within that maximum number of attempts and would return
false otherwise.
Write a description of how you would change the method in the DogAnalysis class in order to support this
modification. Do not write the program code for this change.
Part A – numUntilObedient
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· test the count of all attempts instead of the count of successes in a row
+1 [Skill 3.C] Maintains counter of all commands given (in the context of a loop)
Unit 4 FRQ
· call incorrectly
· call incorrectly
Responses can still earn the point even if they calculate count incorrectly
Responses will not earn the point if they return the number of successes in a row instead of count of commands given
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· the new variable is compared to the count of all commands, either as a loop condition or to determine the return value
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
Unit 4 FRQ
14. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.
This question involves the StringManip class, which is used to perform manipulation on strings.
The class provides the removeSpaces method, whose implementation is not shown. The method takes a string and
returns a new string with spaces removed. For example, removeSpaces("hi how are you") returns
"hihowareyou". The removeSpaces method will be used in part (b).
(a) Write method reverseString, which takes a string str and returns a new string with the characters in str in
reverse order. For example, reverseString("ABCDE") should return "EDCBA".
Complete the reverseString method below by assigning the reversed string to result.
Unit 4 FRQ
For this question, let a palindrome be defined as a string that, when spaces are removed, reads the same forward and
backward. For example, "race car" and "taco cat" are palindromes. You will write method
palindromeChecker, which determines whether a string is a palindrome and prints a message indicating the result.
Examples of the intended behavior of the method are shown in the following table.
(b) Write method palindromeChecker below. Assume that reverseString works as specified, regardless of
what you wrote in part (a). You must use reverseString and removeSpaces appropriately to receive full credit.
Your implementation must conform to the examples in the table.
Points earned:
+1 [Skill 3.A] Examines an individual character in using substring or another appropriate method
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
= 0; j--) Line 5: { Line 6: result += str.substring(j, j + 1); Line 7: } Line 8: return result; Line 9: }">
0 1 2 3 4
Canonical Solution:
Unit 4 FRQ
= 0; j--) Line 5: { Line 6: result += str.substring(j, j + 1); Line 7: } Line 8: return result; Line 9: }">
Points Earned:
+1 [Skill 3.A] Removes spaces from the string to be compared but retains spaces for the string used in printed message
Response earns this point, but incurs general penalty x below if it...
· uses a local variable for the reversed string, but does not declare it.
+1 [Skill 3.A] Compares a string to its reverse using or another appropriate method
+1 [Skill 3.C] Uses a condition statement to print one message when is a palindrome and print another message
otherwise
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
+1 Removes spaces from the string to be compared but retains spaces for the string used in printed
message ( Points Earned )
+1 Calls to obtain a reversed string ( Points Earned )
+1 Compares a string to its reverse using or another appropriate method ( Points Earned )
+1 Uses a condition statement to print one message when is a palindrome and print another message
otherwise ( Points Earned )
+1 Prints messages in specified format ( Points Earned )
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
(General Penalties)
-1 (x) Local variables used but none declared (General Penalties)
-1 (z) Void method or constructor that returns a value (General Penalties)
Canonical Solution:
Unit 4 FRQ
15. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.
The method printNums has two parameters: value and numRounds. The method will iterate for
numRounds rounds. In each round, random integers between 0 and 9, inclusive, are generated and printed on a
single line until value is generated. At that time, value is printed and the round stops. Values for the next
round are printed on the next line of output.
For example, a call to printNums(5, 4) could result in the following output. Each round stops when 5 is
printed for a total of four rounds.
325
7884465
06165
9678971145
Points earned:
+1 [Skill 3.C] Generates the correct number of rounds (no bounds errors).
+1 [Skill 3.C] Uses nested loops to generate sequences of values for all rounds.
+1 [Skill 3.A] Output is printed in the appropriate format, with each round on its own line.
Response earns this point, but incurs general penalty z below if it...
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Unit 4 FRQ
Canonical Solution:
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Unit 4 FRQ
16. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
2. The PrimeNumber class contains methods used with prime numbers. You will write one method of the
PrimeNumber class.
/** Returns the proportion of numbers between start and end, inclusive,
that are
* prime, as described in part (a)
* Precondition
Precondition: 1 < start <= end <= Integer.MAX_VALUE
*/
public static double propOfPrimes(int start, int end)
{ /* to be implemented in part (a) */ }
(a) Write the method propOfPrimes, which returns the proportion of numbers between start and
end, inclusive, that are prime. A helper method, isPrimeNumber, has been provided. The
isPrimeNumber method returns true if its parameter is a prime number and returns false otherwise.
For example, there are numbers between and , inclusive, and of them are prime ( , , , and ), so
the method call PrimeNumber.propOfPrimes(5, 14) returns 0.4.
/** Returns the proportion of numbers between start and end, inclusive,
Unit 4 FRQ
(b) A programmer wants to modify the PrimeNumber class so that the propOfPrimes method always
returns the proportion of numbers between and a given number, inclusive, that are prime.
Write a description of how you would change the PrimeNumber class in order to support this modification. Do
not write the program code for this change.
Part A – propOfPrimes
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.C] Loops through all potential prime numbers between and , inclusive
Responses can still earn the point even if they return early inside the loop
+1 [Skill 3.C] Increments number of primes based on return value (in the context of a loop)
Responses can still earn the point even if they call incorrectly
Responses will not earn the point if they do not initialize the counter
Responses will not earn the point if they use an incorrectly computed denominator
Responses can still earn the point even if they use incorrect values for the numerator or the denominator of the proportion
Responses will not earn the point if they use integer division before casting
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Unit 4 FRQ
Canonical Solution:
0 1 2 3 4 5
+1 Loops through all potential prime numbers between and , inclusive (Points earned)
+1 Calls on a value between and , inclusive (Points earned)
+1 Increments number of primes based on return value (in the context of a loop)
(Points earned)
+1 Determines denominator for proportion calculation (Points earned)
+1 Returns value representing the proportion of primes in range of to (Points
earned)
-1 [penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General penalties)
-1 [penalty] (x) Local variables used but none declared (General penalties)
Canonical Solution:
Unit 4 FRQ
Part B – PrimeNumber
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
o identify reassignment of the parameter to the value 2 inside the method without removing the parameter
o do not indicate to remove a parameter, but indicate the use of the value 2 in place of the parameter and remove all
references to the parameter from the method
· the method would start the loop at 2 and end at the given parameter. The calculation of the proportion would also change
since the count of numbers examined would be one less than the given input value
Responses can still earn the point even if they do not specify a change to the method header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ
17. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. The NumberProperties class contains methods used to determine various properties of numbers. You
will write one method of the NumberProperties class.
/** Returns the ratio of the sum of all the perfect cubes between start
and end,
* inclusive, to the sum of all the perfect squares between start
and end, inclusive,
* as described in part (a)
* Precondition
Precondition: 1 <= start <= end <= Integer.MAX_VALUE
* There is at least one perfect square between start
and end,
* inclusive.
*/
public static double ratioOfCubeSumsToSquareSums(int start, int end)
{ /* to be implemented in part (a) */ }
(a) Write the method ratioOfCubeSumsToSquareSums, which returns the ratio of the sum of all perfect
cubes between start and end, inclusive, to the sum of all perfect squares between start and
Unit 4 FRQ
end, inclusive.
Two helper methods, isSquare and isCube, have been provided. The isSquare method returns true
if its parameter is a perfect square and returns false otherwise. The isCube method returns true if its
parameter is a perfect cube and returns false otherwise.
For example, of the numbers between and , inclusive, two are perfect cubes ( and ) and three are perfect
squares ( , , and ). The sum of the two perfect cubes is and the sum of the three perfect squares is .
The method call NumberProperties.ratioOfCubeSumsToSquareSums(5, 30) returns the ratio of
the sums 35 and 50, which is 0.7.
You must use isSquare and isCube appropriately to receive full credit. Assume that there is at least one
perfect square between start and end, inclusive.
/** Returns the ratio of the sum of all the perfect cubes between start
and end, inclusive,
* to the sum of all the perfect squares between start and end,
inclusive,
* as described in part (a)
* Precondition
Precondition: 1 <= start <= end <= Integer.MAX_VALUE
* There is at least one perfect square between start and
end, inclusive.
*/
public static double ratioOfCubeSumsToSquareSums(int start, int end)
Write a description of how you would change the NumberProperties class in order to support this
modification. Do not write the program code for this change.
Part A – ratioOfCubeSumsToSquareSums
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they return early inside the loop
Unit 4 FRQ
· call or incorrectly
Responses will not earn the point if they do not increment variables for the sums of squares and cubes
+1 [Skill 3.C] Sums all squares and cubes between and (algorithm)
· accumulate using variables that are declared as variables and susceptible to overflow
· assume that a number cannot be both a perfect cube and a perfect square
Responses can still earn the point even if they calculate a return value based on incorrect counts
Responses will not earn the point if they use integer division before casting
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
+1 [Skill 3.B] The response identifies properties of the variable needed to support the change in the method:
· the second parameter of the method would become the number of perfect squares to include
OR
· a new variable of type (or integer) to represent the number of perfect squares to include
· the implementation would change to keep track of how many perfect squares have been found, and the loop would
change so that it terminates when the given number of perfect squares are found
Responses can still earn the point even if they do not specify a change to the
method header
Unit 4 FRQ
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
+1 The response identifies properties of the variable needed to support the change in the method (Points
earned):
o the second parameter of the method would become the number of perfect squares to include
OR
o a new variable of type (or integer) to represent the number of perfect squares to include
+1 The response describes how the method changes (Points earned):
o the implementation would change to keep track of how many perfect squares have been found, and the
loop would change so that it terminates when the given number of perfect squares are found
Unit 4 FRQ
18. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.
This question involves objects of the Kid and Parent classes below.
The determineRelationship method appears in a class other than Kid or Parent. The
determineRelationship method takes Kid objects one and two as parameters. The intended behavior of
the method is described below.
If one and two refer to the same Kid object, the method should print "Same kid".
Regardless of whether one and two refer to the same Kid object, the method should print "Same parent"
if the Parent objects returned by getParent are equal as determined by the equals method.
If the Parent objects returned by getParent are not equal as determined by the equals method, the
method should print "Unrelated".
Unit 4 FRQ
+1 [Skill 3.A] Calls the method on parent objects of and to determine if they have the same parent.
+1 [Skill 3.C] Prints appropriate message based on whether and have the same parent references.
Response earns this point, but incurs general penalty z below if it...
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4
Canonical Solution:
Unit 4 FRQ
Unit 4 FRQ
19. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN
JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
A manufacturer wants to keep track of the average of the ratings that have been submitted for an item using a
running average. The algorithm for calculating a running average differs from the standard algorithm for
calculating an average, as described in part (a).
A partial declaration of the RunningAverage class is shown below. You will write two methods of the
RunningAverage class.
Unit 4 FRQ
(a) Write the method updateAverage, which updates the RunningAverage object to include a new
rating. To update a running average, add the new rating to a calculated total, which is the number of ratings times
the current running average. Divide the new total by the incremented count to obtain the new running average.
For example, if there are ratings with a current running average of , the calculated total is times , or
. When a fifth rating with a value of is included, the new total becomes . The new running average is
divided by , or .
(b) Write the processNewRatings method, which considers num new ratings for inclusion in the running
average. A helper method, getNewRating, which returns a single rating, has been provided for you.
The running average must only be updated with ratings that are greater than or equal to zero. Ratings that are less
than 0 are considered invalid and are not included in the running average.
The processNewRatings method returns the number of invalid ratings. See the table below for three
examples of how calls to processNewRatings should work.
Unit 4 FRQ
Ratings processNewRatings
Statement Comments
0.0,
-2.2, Two new ratings (0.0
and 3.5) are
processNewRatings(4) 2
included in the
3.5, running average.
-1.5
Part A – updateAverage
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
Responses can still earn the point even if they fail to update instance variables
Responses can still earn the point even if the calculated total and/or count is incorrect
Unit 4 FRQ
Responses will not earn the point if the calculation results in a loss of precision (i.e. integer division)
Responses can still earn the point even if they calculate or incorrectly
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 1 2 3
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
Responses can still earn the point even if they call more than once per iteration
· include parameters
Responses can still earn the point even if the comparison with is incorrect
[Skill 3.C] Computes number of invalid ratings and includes only appropriate ratings in the average (algorithm)
Unit 4 FRQ
· return early
Responses can still earn the point even if the returned count is incorrect
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
= 0) Line 8: { Line 9: updateAverage(v); Line 10: } Line 11: else Line 12: { Line 13: invalid++; Line 14:} Line 15: } Line
16: return invalid; Line 17: } end code">
0 1 2 3 4 5 6
Unit 4 FRQ
Canonical Solution:
= 0) Line 8: { Line 9: updateAverage(v); Line 10: } Line 11: else Line 12: { Line 13: invalid++; Line 14:} Line 15: } Line
16: return invalid; Line 17: } end code">
Unit 4 FRQ
20. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
4. This question involves analyzing integer values that are obtained using the getInt method in the following
TargetIntegers class. You will write one method in the class.
(a) Some integer values are considered target numbers. A helper method, isTarget, has been provided.
Method isTarget returns true if its parameter is a target number and returns false otherwise. Some
target numbers are positive, and some are negative.
Unit 4 FRQ
Method runSimulation obtains sampleSize values using the getInt method. The method returns
true if there are more positive target values (target values that are greater than ) in the first half of the obtained
values than in the second half of the obtained values.
Complete method runSimulation. You must use getInt and isTarget appropriately in order to
receive full credit.
(b) A programmer wants to modify the TargetIntegers class so that the number of values to be analyzed by
the runSimulation method is maintained in a variable with a value that cannot change once it is initialized.
Write a description of how you would change the TargetIntegers class in order to support this
modification. Do not write the program code for this change.
Part A – runSimulation
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they do not split the sample into two equal-sized parts
Responses can still earn the point even if they do not use the return value
+1 [Skill 3.C] Compares input values to and calls to identify inputs to be included in counts
· call incorrectly
Unit 4 FRQ
· call incorrectly
+1 [Skill 3.C] Maintains separate counts for each half of integers obtained
· call incorrectly
Responses can still earn the point even if they use incorrectly calculated counts in calculating the return value
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 && isTarget(input)) Line 9: { Line 10: count1++; Line 11: } Line 12: } Line 13 is blank. Line 14: int count2 = 0; Line
15: for (int i = 0; i 0 && isTarget(input)) Line 20: { Line 21: count2++; Line 22: } Line 23: } Line 24 is blank. Line 25:
return (count1 > count2); Line 26: } end code ">
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
0 && isTarget(input)) Line 9: { Line 10: count1++; Line 11: } Line 12: } Line 13 is blank. Line 14: int count2 = 0; Line
15: for (int i = 0; i < sampleSize / 2; i++) Line 16: { Line 17: int input = getInt(); Line 18 is blank. Line 19: if (input > 0
&& isTarget(input)) Line 20: { Line 21: count2++; Line 22: } Line 23: } Line 24 is blank. Line 25: return (count1 >
count2); Line 26: } end code ">
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "integer"
· or "constant"
Responses can still earn the point even if they specify the variable as an instance variable
· would need to change so that it has no parameters and uses the variable in place of the previous
parameter
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
Unit 4 FRQ
0 1 2
Unit 4 FRQ
21. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods
are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes
defined in that question. Writing significant amounts of code that can be replaced by a call to one of these
methods will not receive full credit.
This question involves a game that is played with multiple spinners. You will write two methods in the SpinnerGame
class below.
(a) The spin method simulates a spin of a fair spinner. The method returns a random integer between min and max,
inclusive. Complete the spin method below by assigning this random integer to result.
In each round of the game, the player and the computer each spin a spinner. The player spins a spinner numbered 1 to
10 , inclusive, whereas the computer spins a spinner numbered 2 to 8, inclusive.
Based on the results of the spins, a message is printed in the formats shown in the examples below.
Unit 4 FRQ
If the player obtains a higher result than the computer, the player gains a number of points equal to the positive
difference between the spins. If the computer obtains a higher result than the player, the player loses a number of points
equal to the positive difference between the spins.
In the event of a tie, the player and the computer each spin the spinner a second time. If the sum of the player’s two
spins are greater than the sum of the computer’s two spins, the player gains one point. If the sum of the computer’s two
spins are greater than the sum of the player’s two spins, the player loses one point. In the event of a tie after two spins,
the round is reported as a tie and the player’s score does not change.
Examples of the playRound method’s intended behavior are shown in the following table.
(b) Complete the playRound method below. You must use the spin method appropriately in order to earn full
credit.
Points earned:
+1 [Skill 3.C] Determine the correct number of possible random numbers in the range.
Response earns this point, but incurs general penalty x below if it...
· creates a local variable for the number of random choices, but does not declare it.
+1 [Skill 3.A] Generates a random integer in the determined range, inclusively, and is stored in to be returned.
· redeclares .
Unit 4 FRQ
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 1 2
+1 Determine the correct number of possible random numbers in the range. (Points earned)
+1 Generated random integer is in the determined, inclusive, and is stored in to be returned
(Points earned)
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
(General Penalties)
-1 (x) Local variables used but none declared (General Penalties)
Canonical Solution:
Points earned:
+1 [Skill 3.A] Calls the method appropriately to generate first spins for the player and the computer
Unit 4 FRQ
Response earns this point, but incurs general penalty x below if it…
· Uses local variables for the spin of player and computer without declaring them,
+1 [Skill 3.C] Checks for a tie on the first pair of spins
+1 [Skill 3.C] Reports a player win or a computer win, if appropriate, based on only the first pair of spins
+1 [Skill 3.C] Stores second spins for the player and computer so that sums are available to compare (i.e., does not
overwrite previously generated values)
· does not create new variables for the second computer and player spins. (Note that general penalty x is not incurred
in this case.)
+1 [Skill 3.C] Compares correct sums in the case of a tie based on the first pair of spins
uses appropriate relational operators and arithmetic expression to compare the sums even if the statement is incorrect.
+1 [Skill 3.C] Reports a player win, computer win, or tie, as appropriate, after the second pair of spins
+1 [Skill 3.A] All messages are in the specified format.
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
computerSpin1 + Line 11: computerSpin2) Line 12: { Line 13: System.out.println("You win! 1 points"); Line 14: } Line
15: else if (computerSpin1 + computerSpin2 > Line 16: playerSpin1 + playerSpin2) Line 17: { Line 18:
System.out.println("You lose. -1 points"); Line 19: } Line 20: else Line 21: { Line 22: System.out.println("Tie. 0
points!"); Line 23: } Line 24: } Line 25: else if (result > 0) Line 26: { Line 27: System.out.println("You win! " + result +
Line 28: " points"); Line 29: } Line 30: else Line 31: { Line 32: System.out.println("You lose. " + result + Line 33: "
points"); Line 34: } Line 35: }">
0 1 2 3 4 5 6 7
+1 Calls the method appropriately to generate first spins for the player and the computer (Points
earned)
+1 Checks for a tie on the first pair of spins (Points earned)
+1 Reports a player win or a computer win, if appropriate, based on only the first pair of spins (Points
earned)
+1 Stores second spins for the player and computer so that sums are available to compare (i.e., does not
overwrite previously generated values) (Points earned)
+1 Compares correct sums in the case of a tie based on the first pair of spins (Points earned)
+1 Reports a player win, computer win, or tie, as appropriate, after the second pair of spins (Points
Unit 4 FRQ
earned)
+1 All messages are in the specified format. (Points earned)
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
(General Penalties)
-1 (x) Local variables used but none declared (General Penalties)
-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General Penalties)
-1 (z) Void method or constructor that returns a value (General Penalties)
Canonical Solution:
computerSpin1 + Line 11: computerSpin2) Line 12: { Line 13: System.out.println("You win! 1 points"); Line 14: } Line
15: else if (computerSpin1 + computerSpin2 > Line 16: playerSpin1 + playerSpin2) Line 17: { Line 18:
System.out.println("You lose. -1 points"); Line 19: } Line 20: else Line 21: { Line 22: System.out.println("Tie. 0
points!"); Line 23: } Line 24: } Line 25: else if (result > 0) Line 26: { Line 27: System.out.println("You win! " + result +
Line 28: " points"); Line 29: } Line 30: else Line 31: { Line 32: System.out.println("You lose. " + result + Line 33: "
points"); Line 34: } Line 35: }">
Unit 4 FRQ
22. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
This question involves the WordMatch class, which stores a secret string and provides methods that compare
other strings to the secret string. You will write two methods in the WordMatch class.
(a) Write the WordMatch method scoreGuess. To determine the score to be returned,
scoreGuess finds the number of times that guess occurs as a substring of secret and then multiplies
that number by the square of the length of guess. Occurrences of guess may overlap within secret.
Assume that the length of guess is less than or equal to the length of secret and that guess is not an
Unit 4 FRQ
empty string.
The following examples show declarations of a WordMatch object. The tables show the outcomes of some
possible calls to the scoreGuess method.
Score Calculation:
(Number of
Number of
Return Value of
Substring
Value of guess Substring
Occurrences) x
game.scoreGuess(guess)
Occurrences
(Square of the
Length of guess
guess)
"i" 4 * 1 * 1 = 4 4
"iss" 2 * 3 * 3 = 18 18
"issipp" 1 * 6 * 6 = 36 36
1 * 11 * 11 =
"mississippi" 121
121
Unit 4 FRQ
Score Calculation:
(Number of
Number of
Return Value of
Value of Substring Occurrences)
Substring
guess x
game.scoreGuess(guess)
Occurrences
(Square of the
Length of guess
guess)
"a" 4 * 1 * 1 = 4 4
"aa" 3 * 2 * 2 = 12 12
"aaa" 2 * 3 * 3 = 18 18
"aabb" 1 * 4 * 4 = 16 16
"c" 0 * 1 * 1 = 0 0
(b) Write the WordMatch method findBetterGuess, which returns the better guess of its two String
parameters, guess1 and guess2. If the scoreGuess method returns different values for guess1 and
guess2, then the guess with the higher score is returned. If the scoreGuess method returns the same value
for guess1 and guess2, then the alphabetically greater guess is returned.
The following example shows a declaration of a WordMatch object and the outcomes of some possible calls to
the scoreGuess and findBetterGuess methods.
Unit 4 FRQ
Return
Value
9 1 * 3 * 3
game.scoreGuess("ten");
36 1 * 6 * 6
game.scoreGuess("nation");
game.findBetterGuess("ten",
"nation"
"nation"); "nation" and 9 for "ten", the
guess with the greater score, "nation",
is returned.
9 1 * 3 * 3
game.scoreGuess("con");
9 1 * 3 * 3
game.scoreGuess("cat");
Since scoreGuess returns 9 for both
game.findBetterGuess("con",
"con" "con" and "cat", the alphabetically
"cat");
greater guess, "con", is returned.
Assume that scoreGuess works as specified, regardless of what you wrote in part (a). You must use
scoreGuess appropriately to receive full credit.
Part A – scoreGuess
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
Unit 4 FRQ
Responses can still earn the point even if they only call
[Skill 3.A] Uses a substring of with correct length for comparison with guess
· only call
· use instead of
[Skill 3.C] Loops through all necessary substrings of (no bounds errors)
Responses will not earn the point if they skip overlapping occurrences
[Skill 3.C] Counts number of identified occurrences of within (in the context of a condition involving
both secret and guess)
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. indicates a point
earned and indicates a general or question-specific penalty.
Responses can still earn the point even if they reverse the comparison
· reimplement incorrectly
· use result of as if
· call incorrectly
Unit 4 FRQ
· reverse a comparison
(w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
scoreGuess(guess2)) Line 4: { Line 5: return guess1; Line 6: } Line 7: if (scoreGuess(guess2) > scoreGuess(guess1)) Line
8: { Line 9: return guess2; Line 10: } Line 11: if (guess1.compareTo(guess2) > 0) Line 12: { Line 13: return guess1; Line
14:} Line 15: return guess2; Line 16: } end code">
0 1 2 3 4
Unit 4 FRQ
[penalty] (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition
check) (General Penalties)
[penalty] (x) Local variables used but none declared (General Penalties)
[penalty] (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General
Penalties)
Canonical Solution:
scoreGuess(guess2)) Line 4: { Line 5: return guess1; Line 6: } Line 7: if (scoreGuess(guess2) > scoreGuess(guess1)) Line
8: { Line 9: return guess2; Line 10: } Line 11: if (guess1.compareTo(guess2) > 0) Line 12: { Line 13: return guess1; Line
14:} Line 15: return guess2; Line 16: } end code">
Unit 4 FRQ
23. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.
Notes:
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to
one of these methods will not receive full credit.
2. This question involves analyzing words that are generated using the getWord method in the following
WordTester class. You will write one method in the class.
// There may be variables and other methods that are not shown.
}
(a) Method wordChecker examines n words that are generated by calling the getWord method
repeatedly. The wordChecker method returns true if fewer than percent of the generated words meet
both of the following criteria and returns false otherwise.
Unit 4 FRQ
(b) A programmer wants to modify the WordTester class so that in the wordChecker method, the
percentage checked for can vary between method calls. For example, in one call to wordChecker, the method
might check whether fewer than percent of the words examined start with the letter "I", and in another call
to wordChecker, the method might check whether fewer than percent of the words examined start with the
letter "J". The programmer wants to implement this change without making any changes to the signature of the
wordChecker method or overloading wordChecker.
Write a description of how you would change the WordTester class in order to support this modification. Do
not write the program code for this change.
Part A – wordChecker
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
Responses can still earn the point even if they inappropriately return inside the loop
+1 [Skill 3.C] Uses and return values along with parameters to determine whether return
value meets criteria
· call incorrectly
· use instead of
Unit 4 FRQ
· call incorrectly
+1 [Skill 3.C] Increments count under appropriate conditions (in the context of a loop)
Responses can still earn the point even if they use incorrect logic to guard the increment
+1 [Skill 3.C] Returns a based on a comparison of the calculated proportion and a value ( )
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
Unit 4 FRQ
0 1 2 3 4 5
Canonical Solution:
Unit 4 FRQ
Select a point value to view scoring criteria, solutions, and/or examples and to score the response. +1 indicates a point
earned and -1 indicates a general or question-specific penalty.
· or "decimal" or or "integer"
o do not describe a variable or identify a type, but describe an appropriate identified value as generated only once during
the method call, randomly or from user input
Responses will not earn the point if they describe a change to the method header
· would need to change so that the percentage calculated in the return statement uses the new variable
instead of 0.1
Responses can still earn the point even if they describe a change to the method header
Responses only earn the point if all described components are plausible, accurate, and consistent as related to the problem
0 1 2
Unit 4 FRQ