... number of grades in the range 0–9, array[7] the number of grades in the range 70–79 and array[10] the number of 100 grades.

Fig. 6.6 Bar chart printing program.


 1   // Fig. 6.6: BarChart.java
 2   // Bar chart printing program.
 3
 4   public class BarChart {
 5      public static void main(String[] args) {
 6         int[] array = {0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1};
 7
 8         System.out.println("Grade distribution:");
 9
10         // for each array element, output a bar of the chart
11         for (int counter = 0; counter < array.length; counter++) {
12            // output bar label ("00-09: ", ..., "90-99: ", "100: ")
13            if (counter == 10) {
14               System.out.printf("%5d: ", 100);
15            }
16            else {
17               System.out.printf("%02d-%02d: ",
18                  counter * 10, counter * 10 + 9);
19            }
20
21            // print bar of asterisks

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.