Other 2007 FR Questions FR other years Be Prepared Home
A-1
Part (a)
  /** @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)
  {
    int n = number;
    while (n > 0)
    {
      int d = n % 10;
      if (d == 0 || number % d != 0)
        return false;
      n /= 10;
    }
    return true;
  } 1
Notes:
  1. Exam grading showed that almost half of the students decided to convert number into a string, then extract individual digits from the string and convert them back into ints.  Something like this:
      public static boolean isSelfDivisor(int number)
      {
        String s = String.valueOf(number);
        for (int i = 0; i < s.length(); i++)
        {
          int d = Integer.parseInt(s.substring(i, i+1));
          if (d == 0 || number % d != 0)
            return false;
        }
        return true;
      }
    Correct solutions of this kind did receive full credit.  However, the grading rubric was setup in such a way that flawed solutions using strings were graded more harshly.  In the real world, it would be in bad taste to unnecessarily convert a simpler data item (int) into a more complicated one (String).  In the context of the AP exam, it may be an expedient way to answer the question, if you know exactly what you are doing.

Part (b)
  /** @param start starting point for values to be checked
   *         Precondition: start > 0
   *  @param num the size of the array to be returned
   *         Precondition: num > 0
   *  @return an array containing the first num integers >= start that are self-divisors
   */
  public static int[] firstNumSelfDivisors(int start, int num)
  {
    int[] a = new int[num]; 1
    int count = 0;
    int n = start;
    while (count < num)
    {
      if (isSelfDivisor(n))
      {
        a[count] = n;
        count++;
      }
      n++;
    }
    return a;
  }
Notes:
  1. Do not forget to create the resulting array.

Other 2007 FR Questions | Back to Contents

Copyright © 2007 by Skylight Publishing
support@skylit.com