Computer Science A
2017 FRQ 1 - Homework
- FRQ 1: Digits Class — Homework Assignment
- Overview
- Learning Objectives
- Instructions
- Part A: The Constructor
- Your Solution - Part A
- Task — Part A
- Code Runner Challenge
- Part B: isStrictlyIncreasing Method
- Examples
- Hints
- Your Solution - Part B
- Task — Part B
- Code Runner Challenge
- Task — Combine
- Code Runner Challenge
- Grading Rubric — Total: 9 points
- SUBMIT HOMEWORK HERE (Link also in slack announcments!)
FRQ 1: Digits Class — Homework Assignment
Due: Submit your completed notebook with all code cells passing their test cases.
Overview
This question involves identifying and processing the digits of a non-negative integer.
The Digits class has an instance variable digitList which is an ArrayList<Integer> that stores the individual digits of a number.
Learning Objectives
- Implement a Java class constructor that populates an
ArrayList - Traverse a list and compare adjacent elements
- Handle edge cases (e.g., single digit, zero)
Instructions
- Read each part carefully before writing any code.
- Complete Part A (the constructor) first — Part B depends on it.
- Run each code cell after implementing your solution to verify your output matches the expected results.
- Complete the full challenge at the bottom to combine both parts.
- Do not look at the reference solution until you have attempted all parts.
Grading rubric at the bottom of this notebook.
Part A: The Constructor
Write the constructor for the Digits class. The constructor initializes and fills digitList with the digits from the non-negative integer num. The elements in digitList must be Integer objects representing single digits, and appear in the same order as the digits in num.
Your Solution - Part A
Complete the constructor below. Replace the comment // YOUR CODE HERE with your implementation.
Task — Part A
Implement the Digits constructor in the cell below. Replace // YOUR CODE HERE with your implementation.
Requirements:
- Initialize
digitListas a newArrayList<Integer> - Fill it with the digits of
numin order (left to right) - Handle the special case where
num == 0(result should be[0])
Code Runner Challenge
DigitsPartA
View IPYNB Source
// CODE_RUNNER: DigitsPartA
import java.util.ArrayList;
public class Main {
private ArrayList<Integer> digitList;
/** Constructs a Digits object that represents num.
* Precondition: num >= 0
*/
public Digits(int num) {
digitList = new ArrayList<Integer>();
// YOUR CODE HERE
// Fill digitList with the digits of num in order
}
// Helper method to display the digitList
public ArrayList<Integer> getDigitList() {
return digitList;
}
public static void main(String[] args) {
// Test your constructor
Digits d1 = new Digits(15704);
System.out.println("Digits(15704): " + d1.getDigitList()); // Expected: [1, 5, 7, 0, 4]
Digits d2 = new Digits(0);
System.out.println("Digits(0): " + d2.getDigitList()); // Expected: [0]
Digits d3 = new Digits(9);
System.out.println("Digits(9): " + d3.getDigitList()); // Expected: [9]
}
}
Main.main(null);
Part B: isStrictlyIncreasing Method
Write the Digits method isStrictlyIncreasing. The method returns true if the elements of digitList appear in strictly increasing order; otherwise, it returns false.
A list is considered strictly increasing if each element after the first is greater than (but not equal to) the preceding element.
Examples
| Method call | Value returned | Explanation |
|---|---|---|
new Digits(7).isStrictlyIncreasing() |
true |
Single digit is always strictly increasing |
new Digits(1356).isStrictlyIncreasing() |
true |
1 < 3 < 5 < 6 |
new Digits(1336).isStrictlyIncreasing() |
false |
3 is not less than 3 |
new Digits(1536).isStrictlyIncreasing() |
false |
5 is not less than 3 |
new Digits(65310).isStrictlyIncreasing() |
false |
6 is not less than 5 |
Hints
Hint 1: Loop Strategy
Compare each element with the next element. If you find any pair where the current element is greater than or equal to the next, return `false`.Hint 2: Loop Bounds
If you're comparing element `i` with element `i+1`, make sure your loop stops at `digitList.size() - 1` to avoid an `IndexOutOfBoundsException`.Hint 3: Return Value
If you complete the entire loop without finding any violations, the list is strictly increasing, so return `true`.Your Solution - Part B
Complete the isStrictlyIncreasing method below. The constructor is provided for you.
Task — Part B
Implement the isStrictlyIncreasing method in the cell below. Replace // YOUR CODE HERE with your implementation.
Requirements:
- Return
trueif each element indigitListis strictly greater than the one before it - Return
falseif any element is equal to or less than the preceding element - A single-digit number is always strictly increasing
The working constructor from Part A has been provided for you to allow the code runner to run— focus only on
isStrictlyIncreasing.
Code Runner Challenge
DigitsPartB
View IPYNB Source
// CODE_RUNNER: DigitsPartB
import java.util.ArrayList;
public class Main {
private ArrayList<Integer> digitList;
// Constructor provided — same as Part A (no changes needed here)
public Digits(int num) {
digitList = new ArrayList<Integer>();
if (num == 0) {
digitList.add(0);
}
while (num > 0) {
digitList.add(0, num % 10);
num /= 10;
}
}
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing() {
// YOUR CODE HERE
return true; // Placeholder - replace with your logic
}
public static void main(String[] args) {
// Test your method
System.out.println("Digits(7).isStrictlyIncreasing(): " + new Digits(7).isStrictlyIncreasing()); // Expected: true
System.out.println("Digits(1356).isStrictlyIncreasing(): " + new Digits(1356).isStrictlyIncreasing()); // Expected: true
System.out.println("Digits(1336).isStrictlyIncreasing(): " + new Digits(1336).isStrictlyIncreasing()); // Expected: false
System.out.println("Digits(1536).isStrictlyIncreasing(): " + new Digits(1536).isStrictlyIncreasing()); // Expected: false
System.out.println("Digits(65310).isStrictlyIncreasing(): " + new Digits(65310).isStrictlyIncreasing()); // Expected: false
}
}
Main.main(null);
Task — Combine
Now implement both Digits and isStrictlyIncreasing from scratch without looking back at your previous cells
Requirements:
- Complete both
// YOUR CODE HEREblocks - All four test cases must produce correct output
Code Runner Challenge
DigitsCompleteFillIn
View IPYNB Source
// CODE_RUNNER: DigitsCompleteFillIn
import java.util.ArrayList;
public class Main {
private ArrayList<Integer> digitList;
/** Constructs a Digits object that represents num.
* Precondition: num >= 0
*/
public Digits(int num) {
// YOUR CODE HERE - Part A
}
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing() {
// YOUR CODE HERE - Part B
return true; // Placeholder
}
// Helper method to display the digitList
public ArrayList<Integer> getDigitList() {
return digitList;
}
public static void main(String[] args) {
// Test both parts together
Digits d1 = new Digits(15704);
System.out.println("Digits(15704): " + d1.getDigitList());
System.out.println("isStrictlyIncreasing: " + d1.isStrictlyIncreasing());
Digits d2 = new Digits(1356);
System.out.println("Digits(1356): " + d2.getDigitList());
System.out.println("isStrictlyIncreasing: " + d2.isStrictlyIncreasing());
}
}
Main.main(null);
Grading Rubric — Total: 9 points
Your homework will be graded using the official AP scoring guidelines. Use this rubric to self-assess before submitting.
Part (a) - Digits constructor - 5 points
- +1 Constructs
digitList(initializes theArrayList) - +1 Identifies a digit in
num - +1 Adds at least one identified digit to the list
- +1 Adds all identified digits to the list (must be inside a loop)
- +1 On exit:
digitListcontains all and only the digits ofnumin the correct order
Part (b) - isStrictlyIncreasing - 4 points
- +1 Compares at least one identified consecutive pair of
digitListelements - +1 Correctly determines if a consecutive pair is out of order (must be inside a loop traversing
digitList) - +1 Compares all necessary consecutive pairs (no bounds errors)
- +1 Returns
trueif and only if all consecutive pairs are in order; returnsfalseotherwise
Automatic Deductions
- -2 Uses a confused identifier instead of
digitList
Submission checklist:
- Part A code cell runs and produces the correct output
- Part B code cell runs and produces the correct output
- Full Challenge code cell runs and produces the correct output
- Submitted to google form link with screenshots on an issue, and a few sentences of the main takeaways
SUBMIT HOMEWORK HERE (Link also in slack announcments!)
INCLUDE: Screenshots of all code runners working with correct output, and a few sentences on the main takeaways you got from this lesson and how this will help you on the AP FRQs for the AP exam