The Digits FRQ asked you to work with an integer, an ArrayList filled with Integer objects, and then make a determination on that ArrayList based on its 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 ) {
        digitList.add( 0 );
    } else {
        int n = num;
        while ( n > 0 ) {
            digitList.add( 0, n % 10 );
            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 be either 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 loop 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, digitList would be backwards when we finished.

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

mod and divide

Let’s 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 in to 14 evenly 4 times. \(3 \times 4 = 12\) and \(14 – 12 = 2\) so the remainder is 2.

We can shorted that a bit with \(14 \% 3 = 2\). % is the Java operator for mod, which calculates 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. \(13256 \div 10 = 1325\). The 6 is gone. Remember, in Java when you divide an int the result must also be an int so we’re not going to worry about decimals.

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 {
        digitList.add(0, n % 10);
        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 things to notice here.

First, we’re not looping all the way to the end of digitList. The code stops at the 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 the 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 one number wasn’t greater than the previous value.

Why ClassCube?

Are you spending too much time grading your students' source code?

ClassCube is an online tool for Computer Science teachers that allows you to create lab assignments for your students. They'll submit their code online and ClassCube will automatically grade it for you.

More Info …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>