lasses: AdditionPattern FRQ
C
Testing code:[Link]
ssume that the classes listed in the Java Quick Reference have been imported where
A
appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null
and that methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed
in classes defined in that question. Writing significant amounts of code that can be replaced by
a call to one of these methods will not receive full credit.
This question involves the implementation of the AddtionPatternclass, which generates a
number pattern. The AddtionPatternobject is constructedwith two positive integer
parameters, as described below.
T he first positive integer parameter indicates the starting number in the pattern.
The second positive integer parameter indicates the value that is to be added to obtain each
subsequent number in the pattern.
AddtionPatternclass also supports the followingmethods.
The
urrentNumber
c , which returns the current number inthe pattern
next
, which moves to the next number in the pattern
prev
, which moves to the previous number in the patternor takes no action if there is no
previous number
AddtionPatternobject that is instantiated
T he following table illustrates the behavior of an
by the following statement.
AdditionPattern plus3 = new AdditionPattern(2, 3);
Method Call Value Returned Explanation
(blank if no value)
[Link]();
2 T he current number is initially the
starting number in the pattern.
[Link]();
The pattern adds 3 each time, so
move to the next number in the
pattern (5).
[Link]();
5 The current number is 5.
[Link]();
The pattern adds 3 each time, so
move to the next number in the
pattern (8).
[Link]();
The pattern adds 3 each time, so
move to the next number in the
pattern (11).
[Link]();
p 11
The current number is 11.
[Link]();
Move to the previous number in the
pattern (8).
[Link]();
Move to the previous number in the
pattern (5).
[Link]();
Move to the previous number in the
pattern (2).
[Link]();
p 2
The current number is
2.
[Link]();
T here is no previous number in the
pattern prior to 2, so no action is
taken.
[Link]();
2
The current number is
2.
rite the complete
W AdditionPatternclass. Your implementationmust meet all
specifications and conform to all examples.