Java

OOP GUIDE / WORK

 

DIGITS CLASS

 

Topics

  • Class design
  • ArrayLists

 

Source

  • This question was part of the 2017 AP exam free response questions.  You can find a link to all free response questions on the previous page.

 

 

TASK – PART 1 – SETUP – DIGITS CLASS STARTER CODE

 

Copy and paste the following starter code to your IDE.  A toString() method has been added for testing convenience.

 

 

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) */ }

    

 

     public String toString()

     {

           return digitList.toString();

     }

}

 

 

 

TASK – PART 2 – WORK

 

Click here for a PDF containing the work that you must do. 

 

Do both Part A and Part B in the Question.

 

 

TASK – PART 4 – TESTING YOUR CLASS

 

Copy and paste the following code that will test your Digits class.  Note that the program below matches the examples from the questions so you should compare your results with the results in the examples.  You should also consider adding your own tests.

 

 

public class DigitsTester

{

     public static void main(String[] args)

     {

           //EXAMPLE 1 FROM THE QUESTION

           Digits d1 = new Digits(15704);

           System.out.println(d1);

          

           //EXAMPLE 2 FROM THE QUESTION

           Digits d2 = new Digits(0);

           System.out.println(d2);

          

           //EXAMPLES 3-7 FROM THE QUESTION (PART B)

           System.out.println(new Digits(7).isStrictlyIncreasing());     //true

           System.out.println(new Digits(1356).isStrictlyIncreasing());  //true

           System.out.println(new Digits(1336).isStrictlyIncreasing());  //false

           System.out.println(new Digits(1536).isStrictlyIncreasing());  //false

           System.out.println(new Digits(65310).isStrictlyIncreasing()); //false

     }

}