Digits FRQ Solution

The Digits FRQ asked you to work with an integer, an ArrayList filled with Integer objects, and making a determination on that ArrayList based on contents.

Part A

For Part A you’re implementing the constructor. Below is my solution. It’s probably a little longer than it needs to be, but it does work and the logic is relatively simple.

public Digits(int num) {
digitList = new ArrayList<>();
if (num == 0) {
} else {
int n = num;
while (n > 0) {
n /= 10;
}
}
}


First line instantiates digitList. This is an important step. If you’re implementing a constructor on an AP exam odds are very good that you’ll either be instantiating or setting values, or both, for instance variables.

Next we look and see if num is zero. This was a special case in the problem description in that it should put a zero into digitList.

If num is greater than zero we’ll go into a while loop and pull off each digit individually. the n>0 is why we needed to check for zero first.

In each lop the code gets the remainder of dividing n by 10 using the % operator. That number is added to digitList at the beginning. n%10 gives us the last digit. If we just added it to digitList then digitList would be backwards when we finished.

We then divide n by 10 to chop off the last digit.

mod and divide

Let’s backtrack and look a little more at why we’re modding and dividing by 10 each time. For this you’re going to need to think back to second or third grade when you first learned to divide.

Do you remember remainders? What’s the remainder from $14 \div 3$?

Well, 3 goes into 14 evenly 4 times. $3 \times 4 = 12$ and $14 - 2 = 2$ so the remainder is 2.

We can shorten that a bit with $14 \% 3 = 2$. It’s the java operator for mod, which is roughly the same as the remainder.

The reason that’s important for this problem is that any time you divide a number by 10 the remainder is equal to the last digit. For example, $13256 \% 10 = 6$. This lets us chop off the last digit of a number.

Now that we have the last digit, how do we get rid of it? For that we divide. $1325 \div 10 = 1325$. Remember, in Java when you divide an int by an int you get an int. The decimals go away.

By adding n % 10 to digitList that puts the last digit into the ArrayList. n /= 10 divides n by 10 to get rid of the last digit.

Do While Loop

Had this not shown up on an AP exam, I would have solved it a bit differently. I would have used a do while loop instead. It’s quite a bit shorter. And you could have certainly done it this way. But since a do while loop is out of scope of the AP Java subset, it’s not an expected answer.

public Digits(int num) {
digitList = new ArrayList<>();
int n = num;
do {
n /= 10;
} while (n > 0);
}


Part B

Back from our sidetrack, let’s move on to Part B.

The task for part B was to implement the isStrictlyIncreasing method to determine whether in the list of integers, each number was greater than the previous number. To do that we’re going to loop through digitList.

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


A couple of things to notice here.

First, we’re not looping all the way to the end of digitList. The code stops at the value second from the end. This is important because we’re comparing a value in position i with the value in position i + 1. If the loop went all the way to the end of digitList it would go out of bounds.

And second, we’re comparing numbers with $\ge$ instead of just $\gt$. The problem description stated that each number had to be greater than, not greater than or equal to, the previous value.

If the if is true, then we return false because that means the list isn’t increasing. If we make it all the way through the loop we’ll return true because we never found a case where on number wasn’t greater than the previous value.

This site contains affiliate links. If you click an affiliate link and make a purchase we may get a small commission. It doesn't affect the price you pay, but it is something we must disclose.