FRQ Self Divisor A

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period4 exercises8 graded tests

Files

README.mdDownload

FRQ Self Divisor A

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

The following is part a of a free response question from 2007. It was question 1 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.

Question 1. A positive integer is called a “self-divisor” if every decimal digit of the number is a divisor of the number, that is, the number is evenly divisible by each and every one of its digits. For example, the number 128 is a self- divisor because it is evenly divisible by 1, 2, and 8. However, 26 is not a self-divisor because it is not evenly divisible by the digit 6. Note that 0 is not considered to be a divisor of any number, so any number containing a 0 digit is NOT a self-divisor. There are infinitely many self-divisors.

Part a. Finish writing method isSelfDivisor below, which takes a positive integer as its parameter. This method returns true if the number is a self-divisor; otherwise, it returns false. The main method includes tests to check if this method is working correctly.

public class SelfDivisor
{

    /**
     * @param number the number to be tested Precondition: number > 0
     * @return true if every decimal digit of number is a divisor of number; false
     *     otherwise
     */
    public static boolean isSelfDivisor(int number)
    {
        // part A
    }

    /****************/

    public static void main(String[] args)
    {
        System.out.println("128: " + isSelfDivisor(128));
        System.out.println("26: " + isSelfDivisor(26));
        System.out.println("120: " + isSelfDivisor(120));
        System.out.println("102: " + isSelfDivisor(102));
    }
}

How to solve this problem

The first thing to do is try to solve the examples by hand. The question tells us that 128 should return true, 26 should return false, and any number with a 0 in it should return false.

To check if 128 is a self-divisor we divide 128 by 8, 2, and 1. If 8, 2, and 1 each go into 128 evenly (have a 0 remainder) then the method should return true.

Exercise 1

Try the code

Open the 01-mod-demo1 folder and run TestMod.java. It is an example, so it has no tests.

To check if 26 is a self-divisor we divide 26 by 6 and find that it has a remainder that is greater than 0, so it can’t be a self-divisor and we return false.

Exercise 2

Try the code

Open the 02-mod-demo2 folder and run TestSelfDivisor.java. It is an example, so it has no tests.

To return false if the number has a 0 in it we just have to check if the current digit is a zero and then return false. So, 120 and 102 should both return false.

Hints:

So we need to loop through all the digits in the number one at a time and test if the current digit is 0 and if so return false. Otherwise we need to test if the passed number is evenly divisible (0 remainder) by the current digit. If it isn’t we return false. If we have looped through all the digits and not found a problem return true.

How can we loop through all the digits in a number? We can use x % 10 to get the rightmost digit from a number and x / 10 to remove the rightmost digit from a number. We can also use the remainder operator (%) to test if the number is evenly divisible by the current digit. Run the example code below to see how this works.

Exercise 3

Try the code

Open the 03-mod-demo3 folder and run TestDigits.java. It is an example, so it has no tests.

Algorithm:

We need to loop through all the digits in a number. For example, with 128 the first time through the loop we want to test the 8, then the second time through the loop test the 2, and the last time test the 1. We can use x % 10 to get the rightmost digit and x / 10 to remove the rightmost digit. We are going to need a local variable that holds the current number since each time through the loop we need to remove the rightmost digit. We will initialize the current number to the passed number and then get the rightmost digit each time through the loop. We will test the digit to see if it is zero and if so return false. We will also test to see if the number is not evenly divisible by the digit and return false in this case. We will then remove the rightmost digit from the local variable and test if we should continue the loop.

Check your understanding: Which loop should you use to loop through all the digits of the number? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQselfDivisorA.html#frsda_1

Check your understanding: What should you use as the test in the while loop? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQselfDivisorA.html#frsda_2

Try to write the code for the method isSelfDivisor. When you are ready click “Run” to test your solution. Remember that it should return true for 128, false for 26, false for 120, and false for 102.

Exercise 4

FRQ SelfDivisor: Write the method isSelfDivisor.

Open the 04-lcfrsda5 folder and edit SelfDivisor.java. Press Run to try it; Run Tests checks it (8 tests).

Video - One way to code the solution

There are many possible solutions to this problem. Here is a possible solution’s video for this problem.

The following video is also on YouTube at https://youtu.be/oK1hDTmR3AE. It walks through creating a solution.

For teachers

FRQ Self Divisor A

Students read CSAwesome2's “FRQ Self Divisor A” in CS Room and work through its 4 Java coding exercises (1 graded, 3 examples).

  • Time: 1 class period
  • Tests: 8 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for Unit 2: Selection and Iteration.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercises 01–04, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-mod-demo1TestMod.javaExample, not graded0
02-mod-demo2TestSelfDivisor.javaExample, not graded0
03-mod-demo3TestDigits.javaExample, not graded0
04-lcfrsda5SelfDivisor.javaAutograded8

Teachers can download a reference solution after signing in.