Java Reference
In-Depth Information
/** Provide access to the bin values. **/
public int getValue (int bin) {
if (bin < 0)
return underflows;
else if (bin >= numBins)
return overflows;
else
return bins[bin];
}
}
The applet below creates an instance of BasicHist and uses it to provide a
histogram of the distribution of values generated by a Gaussian random num-
ber generator. (We discuss details about random number generation in Java in
Chapter 4.) We give the histogram ten bins and set the limits from
2.0 to 2.0.
/** Class built from StarterApplet1. **/
public class BasicHistApplet1 extends java.applet.Applet
{
public void init () {
// Create an instance of the Random class for
// producing our random values.
java.util.Random r = new java.util.Random ();
// Create an instance of our basic histogram class.
// Make it wide enough enough to include most of the
// gaussian values.
BasicHist bh = new BasicHist (10, 2.0, 2.0);
// The method nextGaussian () in the class Random
// produces values centered at 0.0 and with a standard
// deviation of 1.0. Use it to fill the histogram
for (int i=0; i < 100; i++) {
double val = r.nextGaussian ();
bh.add (val);
}
// Print out the frequency values in each bin.
for (int i = 0; i < 10; i++) {
System.out.println ( " Bin " +i+ "=" +
bh.getValue (i));
}
Search WWH ::




Custom Search