public class ArithmeticNumberStream implements IncreasingNumberStream
{
private int startValue, nextValue, increment;
public ArithmeticNumberStream(int sv, int inc)
{
startValue = sv;
nextValue = sv;
increment = inc;
}
public int nextTerm() 1
{
int saved = nextValue;
if ((double)nextValue + increment >
(double)Integer.MAX_VALUE) 2
nextValue = Integer.MAX_VALUE;
else
nextValue += increment;
return saved;
}
public void restart() 1
{
nextValue = startValue;
}
Notes:
- Method names are determined by the
IncreasingNumberStream interface
- Cast to
double to avoid arithmetic overflow.
|