How to Solve the Lists of Integers FRQ
Digits Class

This FRQ asks you to work with a list of digits from a non-negative integer. You will complete a constructor and one method for the Digits class.

FRQ Overview

This problem has two main parts:

  1. Part A: Create an ArrayList<Integer> that stores each digit of a number.
  2. Part B: Check whether the digits are in strictly increasing order.

Given Class

import java.util.ArrayList;

public class Digits 
{
    /** The list of digits from the number used to construct this object.
     * The digits appear in the list in the same order in which they appear
     * in the original number.
     */
    private ArrayList<Integer> digitList;

    /** Constructs a Digits object that represents num.
     * Precondition: num >= 0
     */
    public Digits(int num) 
    { 
        /* to be implemented in part (a) */ 
    }

    /** Returns true if the digits in this Digits object are in strictly increasing order;
     * false otherwise.
     */
    public boolean isStrictlyIncreasing() 
    { 
        /* to be implemented in part (b) */ 
    }
}

Part A: Writing the Constructor

Goal

The constructor must take an integer and place each digit into digitList in the same order as the original number.

Example:

Digits d1 = new Digits(15704);

The list should become:

[1, 5, 7, 0, 4]

Important Idea: Using Modulus

The modulus operator % gives the remainder after division. When we use % 10, Java gives us the last digit of a number.

15704 % 10

This gives:

4

The problem is that this finds the digits from right to left. So if we simply add each digit to the end of the list, the digits would be backward.

To keep the digits in the correct order, we add each new digit to the front of the list.

digitList.add(0, digit);

Part A Solution

public Digits(int num)
{
    digitList = new ArrayList<Integer>();

    if (num == 0)
    {
        digitList.add(0);
    }

    while (num > 0)
    {
        int digit = num % 10;
        digitList.add(0, digit);
        num = num / 10;
    }
}

How the Constructor Works

For this object:

new Digits(15704);
num num % 10 Digit Added to Front digitList
15704 4 4 [4]
1570 0 0 [0, 4]
157 7 7 [7, 0, 4]
15 5 5 [5, 7, 0, 4]
1 1 1 [1, 5, 7, 0, 4]

Special Case: The Number 0

If num is 0, the while loop will not run because 0 > 0 is false.

That means we must manually add 0 to the list.

if (num == 0)
{
    digitList.add(0);
}

For new Digits(0), the list should be:

[0]

Part B: Writing isStrictlyIncreasing

Goal

The method isStrictlyIncreasing returns true if every digit is greater than the digit before it.

Strictly increasing means:

1, 3, 5, 6

This is strictly increasing because:

1 < 3 < 5 < 6

Equal digits are not allowed. The next digit must be greater, not equal.

1, 3, 3, 6

This is not strictly increasing because 3 is equal to 3.

Examples

Method Call Return Value Reason
new Digits(7).isStrictlyIncreasing() true One digit is automatically increasing.
new Digits(1356).isStrictlyIncreasing() true 1 < 3 < 5 < 6
new Digits(1336).isStrictlyIncreasing() false 3 is equal to 3.
new Digits(1536).isStrictlyIncreasing() false 5 is greater than 3, so the order breaks.
new Digits(65310).isStrictlyIncreasing() false The digits are decreasing.

Part B Solution

public boolean isStrictlyIncreasing()
{
    for (int i = 1; i < digitList.size(); i++)
    {
        if (digitList.get(i) <= digitList.get(i - 1))
        {
            return false;
        }
    }

    return true;
}

How Part B Works

We start the loop at index 1 because each digit must be compared to the digit before it.

digitList.get(i)

This gets the current digit.

digitList.get(i - 1)

This gets the previous digit.

If the current digit is less than or equal to the previous digit, the list is not strictly increasing.

if (digitList.get(i) <= digitList.get(i - 1))

This catches both problems:

Complete FRQ Solution

import java.util.ArrayList;

public class Digits 
{
    private ArrayList<Integer> digitList;

    public Digits(int num)
    {
        digitList = new ArrayList<Integer>();

        if (num == 0)
        {
            digitList.add(0);
        }

        while (num > 0)
        {
            int digit = num % 10;
            digitList.add(0, digit);
            num = num / 10;
        }
    }

    public boolean isStrictlyIncreasing()
    {
        for (int i = 1; i < digitList.size(); i++)
        {
            if (digitList.get(i) <= digitList.get(i - 1))
            {
                return false;
            }
        }

        return true;
    }
}

Student-Friendly Strategy

Part A Strategy

  1. Create the ArrayList.
  2. Handle the special case when num == 0.
  3. Use % 10 to get the last digit.
  4. Add that digit to the front of the list.
  5. Use / 10 to remove the last digit.
  6. Repeat until the number is gone.

Part B Strategy

  1. Start at index 1.
  2. Compare each digit with the digit before it.
  3. If the current digit is less than or equal to the previous digit, return false.
  4. If the loop finishes, return true.

Common Student Mistakes

Mistake 1: Forgetting to initialize the ArrayList

Incorrect:

digitList.add(0);

Correct:

digitList = new ArrayList<Integer>();
digitList.add(0);

Mistake 2: Adding digits to the end of the list

Incorrect:

digitList.add(digit);

This stores the digits backward.

Correct:

digitList.add(0, digit);

Mistake 3: Forgetting the special case for 0

Incorrect:

while (num > 0)
{
    digitList.add(0, num % 10);
    num = num / 10;
}

This creates an empty list when num is 0.

Correct:

if (num == 0)
{
    digitList.add(0);
}

Mistake 4: Allowing equal digits

Incorrect:

if (digitList.get(i) < digitList.get(i - 1))

This would allow repeated digits.

Correct:

if (digitList.get(i) <= digitList.get(i - 1))

Strictly increasing means each digit must be greater than, not equal to, the previous digit.

Quick Review Questions

  1. What does num % 10 give you?
  2. Why do we use digitList.add(0, digit)?
  3. Why must we handle the special case when num == 0?
  4. Why does the loop in isStrictlyIncreasing start at index 1?
  5. Why does the method use <= instead of only <?