// Kyle Thayer, CSE // This program creates two histograms of the midterm scores // The first is using *'s printed to the console // The second is a drawing using the DrawingPanel import java.io.*; import java.awt.*; import java.util.*; public class MidtermHistogram{ public static final int MAX_SCORE = 101; // max possible score public static void main(String[] args) throws FileNotFoundException { // create a scanner on the input file Scanner input = new Scanner(new File("midterm2.txt")); // create an array of size MAX_SCORE + 1 // this will have indices from 0 - 101, one for each possible score int[] scoreCount = new int[MAX_SCORE + 1]; // loop over the file pulling off each score while(input.hasNext()){ int score = input.nextInt(); // increment the score counter // eg. if this score was 72, we add one to the counter scoreCount[72] scoreCount[score]++; } // loop over the score counts, printing the row for it for(int i = 0; i < scoreCount.length; i++){ // only print lines for scores at least one person recieved if(scoreCount[i] > 0){ System.out.print("score " + i + ": "); // print out scoreCount[i] stars // eg, if 3 people recieved 72's, then scoreCount[72] will be 3 for(int j = 0; j < scoreCount[i]; j++){ System.out.print("*"); } System.out.println(); } } //draw a histogram on a DrawingPanel draw(scoreCount); } // Uses a DrawingPanel to draw the histogram public static void draw(int[] count) { DrawingPanel p = new DrawingPanel(count.length * 6 + 12, 500); Graphics g = p.getGraphics(); g.setColor(Color.BLUE); for (int i = 0; i < count.length; i++) { g.drawLine(i * 6 + 6, 475, i * 6 + 6, 475 - 40 * count[i]); } } }