2016 FRQ 1
answer + explanation for method and control structure 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. 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 /** Constructs a Digits object that represents num. public Digits(int num) { / to be implemented in part (a) / } /** Returns true if the digits in this Digits object are in strictly increasing order; public boolean isStrictlyIncreasing() { / to be implemented in part (b) / } } 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. 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.
public Digits(int num) { // constructor - initializes an object
digitList = new ArrayList<Integer>();
if (num == 0) { // control structure, conditional statement
digitList.add(new Integer(0));
}
while (num > 0) {
digitList.add(0, new Integer(num % 10));
num /= 10;
}
}
public boolean isStrictlyIncreasing() { //use of boolean primitive, creates method
for (int i = 0; i < digitList.size()-1; i++) { // control structure, looping statement
if (digitList.get(i).intValue() >= digitList.get(i+1).intValue()) { // conditional statement
return false;
}
}
return true;
}