LastDigitDistribution.java.html

  1  /**
  2     This class analyzes the distribution of the last digit of values
  3     from a sequence.
  4  */
  5  public class LastDigitDistribution
  6  {
  7     private int[] counters;
  8  
  9     /**
 10        Constructs a distribution whose counters are set to zero.
 11     */
 12     public LastDigitDistribution()
 13     {
 14        counters = new int[10];
 15     }
 16  
 17     /**
 18        Processes values from this sequence.
 19        @param seq the sequence from which to obtain the values
 20        @param valuesToProcess the number of values to process
 21     */
 22     public void process(Sequence seq, int valuesToProcess)
 23     {
 24        for (int i = 1; i <= valuesToProcess; i++)
 25        {
 26           int value = seq.next();
 27           int lastDigit = value % 10;
 28           counters[lastDigit]++;
 29        }
 30     }
 31  
 32     /**
 33        Displays the counter values of this distribution.
 34     */
 35     public void display()
 36     {
 37        for (int i = 0; i < counters.length; i++)
 38        {
 39           System.out.println(i + ": " + counters[i]);
 40        }
 41     }
 42  }