Computer Science A
2017 FRQ Question 2
- Question Overview
- Example 1: Basic Sequence
- Example 2: Edge Cases & Different Patterns
- Code Runner Challenge
- Solution Explanation
- Scoring Guidelines
Question Overview
Design a class that implements an interface to generate a sequence of practice problems. The interface StudyPractice is provided; you must implement the MultPractice class that produces multiplication problems.
StudyPractice Interface
public interface StudyPractice {
String getProblem(); // Returns the current practice problem
void nextProblem(); // Advances to the next practice problem
}
MultPractice Specification
The MultPractice class:
- Constructor: Takes two integers—a constant
firstIntegerand an initialsecondInteger - State: Stores the first integer (unchanging) and the second integer (increments on demand)
- getProblem(): Returns a string in the exact format:
"firstInteger TIMES secondInteger" - nextProblem(): Increments the second integer by 1 each time it’s called
Key Insight
The first integer never changes; only the second integer changes as you call nextProblem(). This is how you generate a sequence of related practice problems.
Example 1: Basic Sequence
This shows the simplest usage—create an object and repeatedly call getProblem() and nextProblem().
Input: MultPractice(7, 3)
| Call | Action | Output |
|---|---|---|
| Create | new MultPractice(7, 3) |
Stores: first=7, second=3 |
| 1 | getProblem() |
“7 TIMES 3” |
| 2 | nextProblem() |
(no output, state changes: second→4) |
| 3 | getProblem() |
“7 TIMES 4” |
| 4 | nextProblem() |
(no output, state changes: second→5) |
| 5 | getProblem() |
“7 TIMES 5” |
| 6 | nextProblem() |
(no output, state changes: second→6) |
| 7 | getProblem() |
“7 TIMES 6” |
What you should see when you run the code:
7 TIMES 3 7 TIMES 4 7 TIMES 5 7 TIMES 6
Example 2: Edge Cases & Different Patterns
This shows unusual but valid usage—demonstrating that your implementation must handle different calling patterns.
Input: MultPractice(4, 12)
| Call | Action | Output |
|---|---|---|
| Create | new MultPractice(4, 12) |
Stores: first=4, second=12 |
| 1 | nextProblem() |
(no output, state changes: second→13) |
| 2 | getProblem() |
“4 TIMES 13” |
| 3 | getProblem() |
“4 TIMES 13” ← Same as before! |
| 4 | nextProblem() |
(no output, state changes: second→14) |
| 5 | nextProblem() |
(no output, state changes: second→15) |
| 6 | getProblem() |
“4 TIMES 15” |
| 7 | nextProblem() |
(no output, state changes: second→16) |
| 8 | getProblem() |
“4 TIMES 16” |
What you should see when you run code with this pattern:
4 TIMES 13 4 TIMES 13 4 TIMES 15 4 TIMES 16
Key differences from Example 1:
- You can call
nextProblem()before ever callinggetProblem()—there’s no requirement to view before advancing - Calling
getProblem()twice in a row returns the same result—it’s read-only; state only changes vianextProblem() - You can call
nextProblem()multiple times to “skip ahead”—it increments by 1 each time
Both examples use the exact same implementation. They just show that your code must be flexible.
Code Runner Challenge
Implement MultPractice
View IPYNB Source
// CODE_RUNNER: Implement MultPractice
interface StudyPractice {
String getProblem();
void nextProblem();
}
class MultPractice implements StudyPractice {
private int firstInteger;
private int secondInteger;
public MultPractice(int firstInteger, int initialSecondInteger) {
this.firstInteger = firstInteger;
this.secondInteger = initialSecondInteger;
}
public String getProblem() {
return firstInteger + " TIMES " + secondInteger;
}
public void nextProblem() {
secondInteger++;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Testing Example 1:");
MultPractice p1 = new MultPractice(7, 3);
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
p1.nextProblem();
System.out.println(p1.getProblem());
}
}
Main.main(null);
Solution Explanation
Here is the complete implementation of the MultPractice class that satisfies all 9 points on the rubric.
Part-by-part Breakdown
Instance Variables (1 point)
private int firstInteger;
private int secondInteger;
- Store the constant multiplier and the incrementing second value
- Both are
privateto encapsulate the state
Constructor (2 points)
public MultPractice(int firstInteger, int initialSecondInteger) {
this.firstInteger = firstInteger;
this.secondInteger = initialSecondInteger;
}
- Takes two parameters and assigns them to instance variables
- Uses
thisto distinguish parameter names from field names
getProblem() (3 points)
public String getProblem() {
return firstInteger + " TIMES " + secondInteger;
}
- Returns a concatenated string with exactly one space before and after
"TIMES" - Uses both instance variables in the correct order
nextProblem() (2 points)
public void nextProblem() {
secondInteger++;
}
- Increments only
secondInteger;firstIntegernever changes - Modifies state without returning anything (void)
How It Works
- When you create
MultPractice(7, 3), it stores 7 and 3. getProblem()returns"7 TIMES 3".nextProblem()changes the second value to 4.- Now
getProblem()returns"7 TIMES 4". - This produces a sequence of related multiplication practice problems with a fixed first factor.
Scoring Guidelines
Points Breakdown (9 points total)
Class Declaration (1 point)
- 1 point: Declares the class header:
public class MultPractice implements StudyPractice - Must include
public, correct class name, andimplements StudyPractice
Instance Variables (1 point)
- 1 point: Declares two private
intinstance variables - Both must be private and of type
int
Constructor (2 points)
- 1 point: Declares constructor header:
public MultPractice(int ___, int ___) - 1 point: Initializes both instance variables using parameters
Method: getProblem() (3 points)
- 1 point: Declares method header:
public String getProblem() - 1 point: Constructs appropriate
Stringfrom instance variables - 1 point: Returns the constructed
Stringin correct format:"firstInteger TIMES secondInteger"
Method: nextProblem() (2 points)
- 1 point: Declares method header:
public void nextProblem() - 1 point: Increments only the second integer instance variable
Common Mistakes to Avoid
- Forgetting the
implements StudyPracticein class declaration - Making instance variables public instead of private
- Incorrect string formatting in
getProblem()(must use" TIMES "with spaces) - Incrementing the wrong variable in
nextProblem() - Using method parameters instead of instance variables