/** A class that creates a histogram from data passed as arguments on the command line. */ public class Histogram { /** Contains the main method. @param args used for command-line arguments */ public static void main(String[] args) { int total; // The total number of values entered at the command line. int[] ranges = new int[10]; // Constructs an array of 10 integer numbers. int[] values = new int[args.length]; // Constructs an array of integer numbers (with the same length as the args array). // Parse each of the String objects and determine the integer values that were passed on the command line. for (int i = 0; i < args.length; i++) values[i] = Integer.parseInt(args[i]); // Determines how many values there are in each specified range. for (int i = 0; i < values.length; i++) { if (values[i] >= 1 && values[i] <= 10) ranges[0]++; if (values[i] >= 11 && values[i] <= 20) ranges[1]++; if (values[i] >= 21 && values[i] <= 30) ranges[2]++; if (values[i] >= 31 && values[i] <= 40) ranges[3]++; if (values[i] >= 41 && values[i] <= 50) ranges[4]++; if (values[i] >= 51 && values[i] <= 60) ranges[5]++; if (values[i] >= 61 && values[i] <= 70) ranges[6]++; if (values[i] >= 71 && values[i] <= 80) ranges[7]++; if (values[i] >= 81 && values[i] <= 90) ranges[8]++; if (values[i] >= 91 && values[i] <= 100) ranges[9]++; } // Prints to the screen a chart that prints an asterisk for each value entered that falls within a specified range. for (int i = 0; i < 10; i++) { System.out.print("\n" + (i * 10 + 1) + (i == 0 ? " " : "") + " - " + (i + 1) * 10 + (i == 9 ? "" : " ") + " | "); for (int j = 0; j < ranges[i]; j++) System.out.print("*"); } // Calculates the total number of values entered at the command line. total = ranges[0] + ranges[1] + ranges[2] + ranges[3] + ranges[4] + ranges[5] + ranges[6] + ranges[7] + ranges[8] + ranges[9]; // Prints to the screen the total number of values entered at the command line. System.out.println("\n\nThe total number of values entered at the command line was: " + total); } }