Other 2017 FR Questions FR other years Be Prepared Home
A-1
Part (a)
  public Digits(int num)
  {
    digitList = new ArrayList<Integer>();
    while (num > 0)
    {
      digitList.add(0, num % 10); 1, 2
      num /= 10;
    }
    if (digitList.size() == 0) 3
      digitList.add(0); 4
  } 5, 6
Notes:
  1. Since digits in num are obtained from right to left, you have to add them at the beginning of digitList.
  2. This is OK due to autoboxing (not in the AP subset, but OK to use); otherwise it would be
        digitList.add(0, new Integer(num % 10));
  3. Don't forget the special case num = 0.
  4. Or, without autoboxing:
        digitList.add(new Integer(0));
  5. You can take care of num = 0 first:
      public Digits(int num)
      {
        digitList = new ArrayList<Integer>();
    
        if (num == 0)
        {
          digitList.add(0);
          return;
        }
    
        while (num > 0)
        {
          digitList.add(0, num % 10);
          num /= 10;
        }
      }
    
  6. Or use a do-while loop to avoid handling 0 separately altogether:
      public Digits(int num)
      {
        digitList = new ArrayList<Integer>();
        do
        {
          digitList.add(0, num % 10);
          num /= 10;
        } while (num > 0);
      }
    


Part (b)
  public boolean isStrictlyIncreasing()
  {
    for (int k = 1; k < digitList.size(); k++)
      if (digitList.get(k).intValue() <= digitList.get(k-1).intValue()) 1
        return false;
    return true;
  }
Notes:
  1. Or, simply,
          if (digitList.get(k) <= digitList.get(k-1))
    due to autounboxing (not in the AP subset, but you can use it).


Other 2017 FR Questions | Back to Contents

Copyright © 2017 by Skylight Publishing
support@skylit.com