// Zorah Fung, CSE 142 // // This program takes an input file of midterm scores ranging from 0 to 10 // and it prints a text and graphical histogram showing the frequencies of // each midterm score. import java.io.*; import java.util.*; public class Histogram { public static final int MAX_SCORE = 100; // max possible score public static final int SHIFT = 5; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("scores.txt")); // read file into count array int[] count = new int[MAX_SCORE + 1]; while (input.hasNextInt()) { int score = Math.min(input.nextInt() + SHIFT, MAX_SCORE); count[score]++; } // use array to produce a histogram for (int score = 0; score < count.length; score++) { // count[i] is the number of people with score i int numPeople = count[score]; System.out.print(score + ": "); // print a * for each person for (int star = 1; star <= count[score]; star++) { System.out.print("*"); } System.out.println(); } } }