Page 419 - Introduction to Programming with Java: A Problem Solving Approach
P. 419
10.6 Problem Solving with Array Case Studies 385
sum -= days[0];
System.arraycopy(days, 1, days, 0, days.length-1);
System.out.print("Enter next day's value: ");
days[days.length-1] = stdIn.nextInt();
sum += days[days.length-1];
Histograms
In this subsection, we’ll use an array as part of a histogram program. But before we present the program, a
histogram overview is in order. A histogram is a graph that displays quantities for a set of categories. Typi-
cally, it indicates category quantities with bars—shorter bars equate to smaller quantities, longer bars equate
to larger quantities. For example, Figure 10.8’s histogram shows quantities of frozen desserts produced in
2
the United States in 2003. Histograms are a popular way to present statistical data because they provide a
quick and clear representation of the data’s distribution.
30 20 10
0 USDA-NASS
Regular Ice Cream
Lowfat Ice Cream
Nonfat Ice Cream
Sherbet
Frozen Yogurt
60
50
40
Frozen Dessert Production February 2003
Apago PDF Enhancer
Figure 10.8 Example histogram
Suppose you have three coins. When you flip all three, you’re curious how likely it is you’ll get zero heads, how likely you’ll get one head, how likely you’ll get two heads, and how likely you’ll get three heads. In other words, you’re curious about the frequency distribution for the number of heads.
You could calculate the frequency distribution mathematically (with the binomial distribution formula), but, instead, you decide to write a program to simulate the coin flips. If you simulate enough coin flips, then the results will approximate the mathemati- cally calculated result.
Approximate a mathematical solution with simulation.
In your program, you should simulate flipping the three coins a million times. You should print the simulation results in the form of a histogram. For each of the four cases (zero heads, one head, two heads,
2 National Agricultural Statistics Service, Frozen Dessert Production Histogram, on the Internet at http://www.usda.gov/nass/ nasskids/glossary_1.html.
Million gallons