Modify the display method of the LastDigitDistribution class of Worked Example 9.1 so that it produces a histogram, like this:

0: *************
1: ******************
2: *************

Scale the bars so that widest one has length 40.

Complete the following file:

LastDigitDistribution.java

/** This class analyzes the distribution of the last digit of values from a sequence. */ public class LastDigitDistribution { private int[] counters; /** Constructs a distribution whose counters are set to zero. */ public LastDigitDistribution() { counters = new int[10]; } /** Processes values from this sequence. @param seq the sequence from which to obtain the values @param valuesToProcess the number of values to process */ public void process(Sequence seq, int valuesToProcess) { for (int i = 1; i <= valuesToProcess; i++) { int value = seq.next(); int lastDigit = value % 10; counters[lastDigit]++; } } /** Displays the counter values of this distribution. */ public void display() { // calculate scale factor ... for (int i = 0; i < counters.length; i++) { // System.out.println(i + ": " + counters[i]); ... } } }

Use the following files:

RandomSequence.java

public class RandomSequence implements Sequence
{
   public int next()
   {
      return (int) (Integer.MAX_VALUE * Math.random());
   }
}

Sequence.java

public interface Sequence
{
   int next();
}

SequenceDemo.java

public class SequenceDemo
{
   public static void main(String[] args)
   {
      LastDigitDistribution dist1 = new LastDigitDistribution();
      dist1.process(new SquareSequence(), 1000);
      dist1.display();
      System.out.println();

      LastDigitDistribution dist2 = new LastDigitDistribution();
      dist2.process(new RandomSequence(), 1000);
      dist2.display();
   }
}

SquareSequence.java

public class SquareSequence implements Sequence
{
   private int n;

   public int next()
   {
      n++;
      return n * n; 
   }
}