// Histogram program developed in class 2/17/2006 // Carl Ebeling import java.io.*; import java.util.*; import java.awt.*; public class Histogram { public static final int MAXSCORE = 105; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("midterm.txt")); int[] count = new int[MAXSCORE+1]; // Process the file while (input.hasNextInt()) { int num = input.nextInt()+5; count[num]++; } textHistogram(count); graphicsHistogram(count); } // Print a text version of the histogram public static void textHistogram(int[] count) { for (int i = 0; i < count.length; i++) { if (count[i] > 0) { System.out.print(i + ": "); printStars(count[i]); System.out.println(); } } } // Print num stars public static void printStars(int num) { for (int j = 0; j < num; j++) { System.out.print("*"); } } // Display a graphics version of the histogram public static void graphicsHistogram(int[] count) { DrawingPanel p = new DrawingPanel(400,300); Graphics g = p.getGraphics(); for (int i = 0; i < count.length; i++) { g.drawLine(3*i, 300, 3*i, 300-(10*count[i]) ); } } }