FRQ No. 1 – Methods and Control Strucutres Overview

Topics Covered

Challenges

  1. Extracting Digits: At first, I didn’t know how to get each digit from a number. I thought about changing the number into words and looking at each letter, but using the % operation seemed smarter.

  2. Starting the List: I was confused about where to start the digitList. Should I do it right at the beginning or when I make a new number? I decided to do it when I make a new number so it’s fresh each time.

  3. I forgot about the number 0. When I tried it, nothing happened! So, I added a special rule just for zero.

  4. Order Matters: In my first try, I only looked if numbers were the same. But then I realized some numbers could be smaller than the one before. I changed my rules to check that.

  5. Getting the Order Right: I was putting numbers at the end of my list, but they were backward! I had to fix it by putting them at the start.

  6. Fast or Slow?: I wondered if my list was slow because I kept adding numbers to the start. I thought about another type of list but kept the one I had. I figured it’s okay since we’re not looking at super long numbers.

  7. Testing: I checked if my code worked, and some numbers were wrong! I had to go back and look at my rules again.

  8. Using the Main Method: I didn’t know how to see if my code was right. After looking it up, I learned about the main method and used that.

  9. Writing Comments: I wasn’t sure how much to write about my code. I wanted to explain things, but not too much.

Past AP Exam FRQ: 2017 #1

Part A

public Digits(int num) {
    // This part initializes an instance variable digit list, as an empty array list of an integer type
    digitList = new ArrayList<Integer>();

    // Handling special case for Zero. This given condition checks if the number is = to 0 and if it is then it directly adds 0 to digitlist. 
    // While loop does not count in for 0 so this is needed here
    if (num == 0) {
        digitList.add(0); 
    }

     // Inside the while loop, the modulus operation is used to get the last digit of the number, 
     // since we we do modulus it gets the decimal place and then it extracts it and adds each 
     // extracted digit to the beggining of digit list. For example 123, the digit would be 3. 
     // Then removing the last digit is what we need to do so,  the number is divided by 10 which truncates the last digit removing it. 
     // So if num was 123, after this line it would become 12
     // The condition num > 0 ensures that this continues only while there are digits left in num.
    while (num > 0) { 
        int digit = num % 10; 
        digitList.add(0, digit); 
        num = num / 10; 
    }
}

Part B

public boolean isStrictlyIncreasing() {
    for (int i = 0; i < digitList.size() - 1; i++) { // This line declares a method which will reutrn a boolean value (true or false) and the purpose of this is the see if the digits in digitList are in increasing order.
        if (digitList.get(i) >= digitList.get(i + 1)) { // for loop that iterates over the elements of digit List. The loops variable i starts at 0 and then goes up till the second to last digit in the element list. 
        // We only go till second to last because we compare the digit to the one next to it and the last digit does not have anything to compare to.
            return false; // output
        }
    }
    return true; // output
}

Runable Script

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 = 0; i < digitList.size() - 1; i++) {

            if (digitList.get(i) >= digitList.get(i + 1)) {
                return false; 
            }
        }
        return true; 
    }


    public static void main(String[] args) { // This line defines the main method and serves as the entry point of the program. There are three instances of digits being created.
    // The digits constructer is invoked which will break down each number into its indivudual digits and store them in digitList. Discusses this above. 
        Digits d1 = new Digits(12345);
        Digits d2 = new Digits(13579);
        Digits d3 = new Digits(13379);

        
        // This line calls the strictly increasing function method on the broken down number, 12345 in this case. With the function it checks if the numbers are in strictly increasing order. Result true is then printed
        // This is now done in every digits 2 and digits 3
        System.out.println(d1.isStrictlyIncreasing());  
        System.out.println(d2.isStrictlyIncreasing()); 
        System.out.println(d3.isStrictlyIncreasing());  
    }
}
Digits.main(null) // Way to run the main method. 
true
true
false

Steps

  1. We first import the ArrayList utility.
  2. Define the Digits class.
  3. Initialize an ArrayList called digitList which will store the digits of a given number.
  4. The constructor (Digits(int num)) populates the digitList based on the given number.
  5. The isStrictlyIncreasing method checks if the digits in digitList are in strictly increasing order.
  6. Finally, a main method has been added to test our class. It creates a few instances of the Digits class and checks if the numbers are strictly increasing.