Java program – Histogram

by Nideesh C on April 29, 2011 · 0 comments

in Java Programming




public class Histogram {
    private final double[] freq;   // freq[i] = # occurences of value i
    private double max;            // max frequency of any value

    // Create a new histogram.
    public Histogram(int N) {
        freq = new double[N];
    }

    // Add one occurrence of the value i.
    public void addDataPoint(int i) {
        freq[i]++;
        if (freq[i] > max) max = freq[i];
    } 

    // draw (and scale) the histogram.
    public void draw() {
        StdDraw.setYscale(0, max);
        StdStats.plotBars(freq);
    }

    // See Program 2.2.6.
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);   // number of coins
        int T = Integer.parseInt(args[1]);   // number of trials

        // create the histogram
        Histogram histogram = new Histogram(N+1);
        for (int t = 0; t < T; t++) {
            histogram.addDataPoint(Bernoulli.binomial(N));
        }

        // display using standard draw
        StdDraw.setCanvasSize(500, 100);
        histogram.draw();
    }
} 


/*************************************************************************
 *
 *  This data type supports simple client code to create dynamic
 *  histograms of the frequency of occurrence of values in [0, N).
 *  The frequencies are kept in an instance-variable array, and
 *  an instance variable max tracks the maximum frequency (for scaling).
 *
 *  % java Histogram 50 1000000
 *
 *************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Bernoulli trials
  2. Java program – Recursive graphics
  3. Java program – Visualization client
  4. Java program – Albers squares
  5. Java program – Data analysis library

Leave a Comment

Previous post:

Next post: