This FRQ asks you to work with a list of digits from a non-negative integer.
You will complete a constructor and one method for the Digits class.
This problem has two main parts:
ArrayList<Integer> that stores each digit of a number.import java.util.ArrayList;
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)
{
/* to be implemented in part (a) */
}
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing()
{
/* to be implemented in part (b) */
}
}
The constructor must take an integer and place each digit into digitList
in the same order as the original number.
Example:
Digits d1 = new Digits(15704);
The list should become:
[1, 5, 7, 0, 4]
The modulus operator % gives the remainder after division.
When we use % 10, Java gives us the last digit of a number.
15704 % 10
This gives:
4
The problem is that this finds the digits from right to left. So if we simply add each digit to the end of the list, the digits would be backward.
To keep the digits in the correct order, we add each new digit to the front of the list.
digitList.add(0, digit);
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;
}
}
For this object:
new Digits(15704);
| num | num % 10 | Digit Added to Front | digitList |
|---|---|---|---|
| 15704 | 4 | 4 | [4] |
| 1570 | 0 | 0 | [0, 4] |
| 157 | 7 | 7 | [7, 0, 4] |
| 15 | 5 | 5 | [5, 7, 0, 4] |
| 1 | 1 | 1 | [1, 5, 7, 0, 4] |
If num is 0, the while loop will not run because
0 > 0 is false.
That means we must manually add 0 to the list.
if (num == 0)
{
digitList.add(0);
}
For new Digits(0), the list should be:
[0]
The method isStrictlyIncreasing returns true
if every digit is greater than the digit before it.
Strictly increasing means:
1, 3, 5, 6
This is strictly increasing because:
1 < 3 < 5 < 6
Equal digits are not allowed. The next digit must be greater, not equal.
1, 3, 3, 6
This is not strictly increasing because 3 is equal to 3.
| Method Call | Return Value | Reason |
|---|---|---|
new Digits(7).isStrictlyIncreasing() |
true |
One digit is automatically increasing. |
new Digits(1356).isStrictlyIncreasing() |
true |
1 < 3 < 5 < 6 |
new Digits(1336).isStrictlyIncreasing() |
false |
3 is equal to 3. |
new Digits(1536).isStrictlyIncreasing() |
false |
5 is greater than 3, so the order breaks. |
new Digits(65310).isStrictlyIncreasing() |
false |
The digits are decreasing. |
public boolean isStrictlyIncreasing()
{
for (int i = 1; i < digitList.size(); i++)
{
if (digitList.get(i) <= digitList.get(i - 1))
{
return false;
}
}
return true;
}
We start the loop at index 1 because each digit must be compared to the digit before it.
digitList.get(i)
This gets the current digit.
digitList.get(i - 1)
This gets the previous digit.
If the current digit is less than or equal to the previous digit, the list is not strictly increasing.
if (digitList.get(i) <= digitList.get(i - 1))
This catches both problems:
3, 35, 3import 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 = 1; i < digitList.size(); i++)
{
if (digitList.get(i) <= digitList.get(i - 1))
{
return false;
}
}
return true;
}
}
ArrayList.num == 0.% 10 to get the last digit./ 10 to remove the last digit.1.false.true.Incorrect:
digitList.add(0);
Correct:
digitList = new ArrayList<Integer>();
digitList.add(0);
Incorrect:
digitList.add(digit);
This stores the digits backward.
Correct:
digitList.add(0, digit);
Incorrect:
while (num > 0)
{
digitList.add(0, num % 10);
num = num / 10;
}
This creates an empty list when num is 0.
Correct:
if (num == 0)
{
digitList.add(0);
}
Incorrect:
if (digitList.get(i) < digitList.get(i - 1))
This would allow repeated digits.
Correct:
if (digitList.get(i) <= digitList.get(i - 1))
Strictly increasing means each digit must be greater than, not equal to, the previous digit.
num % 10 give you?digitList.add(0, digit)?num == 0?isStrictlyIncreasing start at index 1?<= instead of only <?