FRQ 1 Question

2017 APCSA FRQ

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

a) 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. Each of the following examples shows the declaration of a Digits object and the contents of digitList as initialized by the constructor.

  • Digits d1 = new Digits(15704);
  • digitList: 1, 5, 7, 0, 4

b) ) 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. The following table shows the results of several calls to isStrictlyIncreasing

FRQ 1 My Solution

(Outline for the Digits class given, added code - constructor for Digits and isStrictlyIncreasing method)

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) { 
        // create the array list of integers 
        digitList = new ArrayList<Integer> ();
        // take what is passed into the constructor and convert it to a string
        String number = String.valueOf(num);
        // Loop through the string and add each character converted integer into the integer array list
        for (int i = 0; i < number.length(); i++) {
            digitList.add(Integer.parseInt(String.valueOf(number.charAt(i))));
        }
        // print out the array list
        System.out.println(digitList);
     }

    /** Returns true if the digits in this Digits object are in strictly increasing order;
     * false otherwise.
     */ 

    public boolean isStrictlyIncreasing() 
    {   // loop through the entire array list of integers
         for (int i = 0; i < digitList.size(); i++) {
            // if the term is not less than (so greater than or equal to) the next term, return false
            if (digitList.get(i) >= digitList.get(i+1)) {
                return false;
            }
         }
         // if every number is greater than the next and so the prev if statement is false, return true
         return true;
    } 

}

Testing the code

private ArrayList<Integer> digitList; 
digitList = new ArrayList<Integer> ();

//expect to get an out put of a list of 12345 and true since the numbers are increasing
String number = String.valueOf(12345);

for (int i = 0; i < number.length(); i++) {
    digitList.add(Integer.parseInt(String.valueOf(number.charAt(i))));
}
System.out.println(digitList); 

for (int i = 0; i < digitList.size() - 1; i++) {
    if (digitList.get(i) >= digitList.get(i+1)) {
        System.out.println("false"); 
        //note: would be a return statement so would exit out of the loop
    }
 }
 System.out.println("true");
[1, 2, 3, 4, 5]
true

Scoring Notes

  • For part A, solution guide didn't use casting to solve the question. Instead, takes the number passed and performs modulus by 10. So for example if 12345 was passed in, 12345 % 10 is 5, the last integer in the number. Then num was divided by 10 for the next iteration of the loop and was still an integer not a double so 12345 became 1234 for the next iteration. Code below. (5/5 ?)

  • Part b was correct (4/4)

public Digits(int num)
{
    digitList = new ArrayList<Integer>();
    if (num == 0)
        {
            digitList.add(new Integer(0));
        }
    while (num > 0)
        {
            digitList.add(0, new Integer(num % 10));
            num /= 10;
        }
}