001package stdlib; 002 003/* *********************************************************************** 004 * This data type supports simple client code to create dynamic 005 * histograms of the frequency of occurrence of values in [0, N). 006 * The frequencies are kept in an instance-variable array, and 007 * an instance variable max tracks the maximum frequency (for scaling). 008 *************************************************************************/ 009 010public class XHistogram { 011 private final double[] freq; // freq[i] = # occurences of value i 012 private double max; // max frequency of any value 013 014 // Create a new histogram. 015 public XHistogram(int N) { 016 freq = new double[N]; 017 } 018 019 // Add one occurrence of the value i. 020 public void addDataPoint(int i) { 021 freq[i]++; 022 if (freq[i] > max) max = freq[i]; 023 } 024 025 // draw (and scale) the histogram. 026 public void draw() { 027 StdDraw.setYscale(0, max); 028 StdStats.plotBars(freq); 029 } 030}