Advertisement
CyrusVerkest

lastDigitDistribution

Jan 29th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement