2017年AP计算机简答题真题PDF下载
1. This question involves identifying and processing the digits of a non-negative integer. The declaration of the Digits class is shown below. You will write the constructor and one method for the Digits class.
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 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) */ }
}
(a) Write the constructor for the Digits class. The constructor initializes and fills digitList with the digits from the non-negative integer num. The elements in digitList must be Integer objects representing single digits, and appear in the same order as the digits in num. Each of the following examples shows the declaration of a Digits object and the contents of digitList as initialized by the constructor.
Complete the Digits constructor below.
/** Constructs a Digits object that represents num.
* Precondition: num >= 0
*/
public Digits(int num)
(b) Write the Digits method isStrictlyIncreasing. The method returns true if the elements of digitList appear in strictly increasing order; otherwise, it returns false. A list is considered strictly increasing if each element after the first is greater than (but not equal to) the preceding element. The following table shows the results of several calls to isStrictlyIncreasing.
Complete method isStrictlyIncreasing below.
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing()
2017年AP计算机简答题真题余下省略!
你可能还关注