/* CSE 142, Autumn 2007, Marty Stepp This program reads an input file of test scores (integers) and displays a histogram of the score distribution. */ import java.awt.*; import java.io.*; import java.util.*; public class Histogram { public static void main(String[] args) throws FileNotFoundException { // tally up all the scores into an array // e.g. tally[83] stores how many people got an 83 on the exam Scanner input = new Scanner(new File("scores.txt")); int[] tally = new int[101]; while (input.hasNextInt()) { int score = input.nextInt(); tally[score]++; } // now that we've finished counting, print the results for (int i = 0; i < tally.length; i++) { if (tally[i] > 0) { System.out.print(i + ": "); // print tally[i] number of stars; // for example, if 4 people got an 83, then print 4 stars for (int j = 0; j < tally[i]; j++) { System.out.print("*"); } System.out.println(); } } // draw a graphical histogram of the data DrawingPanel p = new DrawingPanel(400, 400); Graphics g = p.getGraphics(); for (int i = 0; i < tally.length; i++) { // each line is 4 px apart, starts at the bottom of the panel (y=400), // and goes up by 15 pixels for each person who got that exam score g.drawLine(i * 4, 400, i * 4, 400 - (15 * tally[i])); } } }