/* CSE 142, Winter 2006 Friday, February 17 2006 lecture This program reads an input file of exam score data and displays a histogram of the data, showing how many students earned each unique exam score. Author: Marty Stepp (stepp at u washington edu) */ import java.awt.*; import java.io.*; import java.util.*; public class Histogram { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("930.txt")); // tally the exam scores into an array (for example, // counts[82] stores number of students who got 82 on exam) int[] counts = new int[101]; while (input.hasNextInt()) { int score = input.nextInt(); counts[score]++; } // set up a drawing panel to draw a graphical histogram DrawingPanel panel = new DrawingPanel(300, 150); Graphics g = panel.getGraphics(); // print and draw each for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { System.out.print((i + 5) + ": "); // adds 5-point curve // print a star counts[i] times (draws a star for each // person who scored 'i' points on the exam) for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); // draw one vertical line on the graphical histogram // (scaled horizontally by 2 and vertically by 5) g.drawLine(2 * i, 100, 2 * i, 100 - (5 * counts[i])); } } } }