Sprint View

2017 FRQ 1

7 min read

FRQ 1: Digits Class

This question involves identifying and processing the digits of a non-negative integer. You will write the constructor and one method for the Digits class.

The Digits class has an instance variable digitList which is an ArrayList<Integer> that stores the individual digits of a number.


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.

Examples

Example 1:

Code Runner Challenge

DigitsPartA

View IPYNB Source
Digits d1 = new Digits(15704);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Result: digitList = [1, 5, 7, 0, 4]

Example 2:

Code Runner Challenge

DigitsPartB

View IPYNB Source
Digits d2 = new Digits(0);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Result: digitList = [0]

Hints

Hint 1: Approach Options There are two main approaches: 1. String approach: Convert the number to a String and extract each character 2. Math approach: Use modulo (`%`) and division (`/`) to extract digits from right to left
Hint 2: String Approach Details - Use `String.valueOf(num)` to convert the integer to a String - Loop through each character using `substring(i, i+1)` - Convert each character back to an integer using `Integer.parseInt()`
Hint 3: Math Approach Details - `num % 10` gives you the last digit - `num / 10` removes the last digit - You'll need to add digits at the beginning of the list (index 0) since you extract from right to left

Your Solution - Part A

Complete the constructor below. Replace the comment // YOUR CODE HERE with your implementation.

// CODE_RUNNER: <challenge text>

Code Runner Challenge

DigitsCompleteFillIn

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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

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.

// CODE_RUNNER: <challenge text>

Code Runner Challenge

DigitsSolution

View IPYNB Source
// CODE_RUNNER: DigitsPartB

import java.util.ArrayList;

public class Main {
    private ArrayList<Integer> digitList;

    public Digits(int num) {
         // Insert your code here!

        }

    /** 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);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Complete Solution

Once you’ve completed both parts, try implementing the full Digits class from scratch below.

// CODE_RUNNER: <challenge text>

// 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);

// CODE_RUNNER: <challenge text>

// CODE_RUNNER: DigitsSolution

import java.util.ArrayList;

public class Main {

    private ArrayList<Integer> digitList;

    /** Constructs a Digits object that represents num.
     * Precondition: num >= 0
     */
    public Main(int num) {
        // YOUR CODE HERE - Part A
        digitList = new ArrayList<Integer>(); // Initialize the list 
        
        // Handle case for 0 as per example 2 [cite: 67, 71]
        if (num == 0) {
            digitList.add(0);
        }
        
        // Extract digits and add to the front of the list to maintain order [cite: 50]
        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 - Part B
        // Iterate and compare each digit to the next 
        for (int i = 0; i < digitList.size() - 1; i++) {
            if (digitList.get(i) >= digitList.get(i + 1)) {
                return false; // Return false if not strictly increasing 
            }
        }
        
        return true;
    }
    
    // Helper method to display the digitList
    public ArrayList<Integer> getDigitList() {
        return digitList;
    }

    public static void main(String[] args) {
        // Test both parts together
        Main d1 = new Main(15704); // Using Main constructor
        System.out.println("Digits(15704): " + d1.getDigitList());
        System.out.println("isStrictlyIncreasing: " + d1.isStrictlyIncreasing());

        Main d2 = new Main(1356);
        System.out.println("Digits(1356): " + d2.getDigitList());
        System.out.println("isStrictlyIncreasing: " + d2.isStrictlyIncreasing());
    }
}

Main.main(null);

2017 FRQ 1: Digits

Part (a) - Digits constructor - 5 points

Intent: Initialize instance variable using passed parameter

  • +1 Constructs digitList

  • +1 Identifies a digit in num

  • +1 Adds at least one identified digit to a list

  • +1 Adds all identified digits to a list (must be in context of a loop)

  • +1 On exit: digitList contains all and only digits of num in the correct order

Part (b) - isStrictlyIncreasing - 4 points

Intent: Determine whether or not elements in digitList are in increasing order

  • +1 Compares at least one identified consecutive pair of digitList elements

  • +1 Determines if a consecutive pair of digitList is out of order (must be in context of a digitList traversal)

  • +1 Compares all necessary consecutive pairs of elements (no bounds errors)

  • +1 Returns true iff all consecutive pairs of elements are in order; returns false otherwise

Question-Specific Penalties

  • -2 (q) Uses confused identifier instead of digitList

Original FRQ

Scoring Guidelines

Key Concepts Tested

  • ArrayList operations: Creating, adding elements, accessing elements with get()
  • String manipulation: Converting integers to strings, extracting substrings
  • Loop iteration: Iterating through collections, comparing adjacent elements
  • Conditional logic: Returning boolean values based on conditions
  • Edge cases: Handling single-digit numbers and zero

Course Timeline