JAVA 7.1.3 Programming Challenge: FRQ Digits
This programming challenge is based on the 2017 Free Response Question part 1a on the 2017 AP CS A exam. In this question, you are asked to write a constructor for a class called Digits. This constructor takes an integer number as its argument and divides it up into its digits and puts the digits into an ArrayList. For example, new Digits(154) creates an ArrayList with the digits [1, 5, 4].
First, let's discuss how to break up a number into its digits. Try the code below. What happens if you divide an integer by 10? Remember that in integer division, the result truncates (cuts off) everything to the right of the decimal point. Which digit can you get by using mod 10, which returns the remainder after dividing by 10? Try a different number and guess what it will print and then run to check.
This is the code that is already given:
public class DivideBy10 {
public static void main(String[] args) {
int number = 154;
System.out.println(number / 10);
System.out.println(number % 10);
}
}