Other 2009 FR Questions FR other years Be Prepared Home
AB-3
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:
  1. Method names are determined by the IncreasingNumberStream interface
  2. Cast to double to avoid arithmetic overflow.

Other 2009 FR Questions | Back to Contents

Copyright © 2009 by Skylight Publishing
support@skylit.com