// Reads an input file of test scores (integers) and displays a // graphical histogram of the score distribution. import java.awt.*; import java.io.*; import java.util.*; public class Histogram2 { public static final int CURVE = 7; // adjustment to each exam score public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("midtermA.txt")); // array of counters of test scores from 0 - 100 inclusive int[] counts = new int[101]; // read file into counts array // example: if score is 87, then do counts[87]++ while (input.hasNextInt()) { int score = input.nextInt(); // curve the exam score (but cap it at 100 points) score = Math.min(score + CURVE, 100); counts[score]++; } // print stars for each student who got a particular score // example: if counts[87] is 6, print "87: ******" for (int i = 0; i < counts.length; i++) { if (counts[i] > 0) { System.out.print(i + ": "); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); } } // use a DrawingPanel to draw the histogram DrawingPanel p = new DrawingPanel(counts.length * 3 + 6, 200); Graphics g = p.getGraphics(); g.setColor(Color.BLACK); for (int i = 0; i < counts.length; i++) { g.drawLine(i * 3 + 3, 175, i * 3 + 3, 175 - 5 * counts[i]); } } }