// Arup Guha
// 7/14/05
// This program asks the user for a data file of grades, and then produces
// a histogram of those grades. This example utilizes static methods.

import java.io.*;
import java.util.Scanner;

public class TestResults {

  private final static int SIZE_GROUPS = 10;

  public static void main(String[] args) throws IOException {

    Scanner stdin = new Scanner(System.in);

    // Get the grade file.
    System.out.println("Which file has your grade data?");
    String filename = stdin.next();

    Scanner fin = new Scanner(new File(filename));

    // Store the grades into the array from the file.
    int[] myGrades = getGrades(fin);
    fin.close();

    // Store the grouped frequency data.
    int[] groups = getGroupedData(myGrades);

    // Output the results.
    System.out.println("Here is a histogram of your class grades:\n");
    printChart(groups);    

  }

  // Precondition: fin points to a file that stores grades.
  // Postcondition: An array will be returned storing each individual
  //                grade from the grade file.
  public static int[] getGrades(Scanner fin) throws IOException {

    // Get the number of grades from the first line in the file.
    int numgrades = fin.nextInt();
    int[] grades = new int[numgrades];

    // Read in all of these grades.
    for (int i=0; i<grades.length; i++) 
      grades[i] = fin.nextInt();

    return grades; // Return the result.
  }

  // Precondition: freq is a frequency array, storing the number of grades
  //               that occurred in each grade range.
  // Postcondition: A chart printing out the information in freq is 
  //                printed out to the screen.
  public static void printChart(int[] freq) {

    // Print out chart heading.
    System.out.println("Grade\tFrequency");
    System.out.println("-----\t---------");
    
    // Loop through each row.
    for (int row=0; row<freq.length; row++) {

      // Print out the grade range for this row.
      System.out.print((row*SIZE_GROUPS)+"-"+((row+1)*SIZE_GROUPS-1)+"\t");

      // Print out one star per score in this group.
      for (int col=0; col<freq[row]; col++)
        System.out.print("*");
      System.out.println();

    }
  }

  // Precondition: all_scores stores all the grades for the class.
  // Postcondition: The grouped frequency information will be stored in
  //                the returned array.
  public static int[] getGroupedData(int[] all_scores) {

    // Determine the grouped frequency array size.
    int array_size = 100/SIZE_GROUPS+1;
    int[] freq = new int[array_size];

    // Initialize that array.
    for (int i=0; i<freq.length; i++)
      freq[i] = 0;
    
    // Loop through all the scores.
    for (int i=0; i<all_scores.length; i++) {

      // Only count scores in between 0 and 100, inclusive.
      if (all_scores[i] >= 0 && all_scores[i] <= 100) 
 
        // Increment the appropriate group's frequency.
        freq[all_scores[i]/SIZE_GROUPS]++;
    }

    return freq; // Return the answer.
  }
}
