// This program calculates and reports various statistics about the results of // a CSE142 midterm exam. // // Notice the use of arrays throughout the program, and specifically the various // array traversals. import java.util.*; import java.io.*; public class Midterm { public static final int MAX_SCORE = 101; public static void main(String[] args) throws FileNotFoundException { Scanner console = new Scanner(System.in); System.out.print("Input file: " ); String inFile = console.nextLine(); Scanner input = new Scanner(new File(inFile)); int[] scores = getScores(input); int[] counts = getCounts(scores); int mode = getMode(counts); double mean = getMean(scores); printStats(scores, counts, mean, mode); drawHistogram(counts); System.out.println(); int[] buckets = getBucketedCounts(scores, 10); System.out.println("Counts: " + Arrays.toString(buckets)); drawBucketedHistogram(buckets, 10); } // Reads in scores from the given Scanner and returns an array // containing each individual score. Assumes the first value in // the input is the number of scores to be read. // // Scanner input - the Scanner from which to read scores public static int[] getScores(Scanner input) { int[] scores = new int[input.nextInt()]; for (int i = 0; i < scores.length; i++) { int nextScore = input.nextInt(); scores[i] = nextScore; } return scores; } // Computes and returns the number of times each individual score was // earned on the exam. // // int[] scores - the individual exam scores public static int[] getCounts(int[] scores) { int[] counts = new int[MAX_SCORE + 1]; for (int i = 0; i < scores.length; i++) { int score = scores[i]; counts[score]++; } return counts; } // Computes and returns the mode (most common score) on the exam. // // int[] counts - the number of times each score was earned public static int getMode(int[] counts) { int maxIndex = 0; for (int i = 0; i < counts.length; i++) { if (counts[i] > counts[maxIndex]) { maxIndex = i; } } return maxIndex; } // Computes and returns the mean score on the exam. // // int[] scores - the scores public static double getMean(int[] scores) { int total = 0; for (int i = 0; i < scores.length; i++) { total += scores[i]; } return (double) total / scores.length; } // Computes and prints the number of each score and the exam mean and median. // // int[] counts - the number of times each score was earned public static void printStats(int[] scores, int[] counts, double mean, int mode) { System.out.println("Scores: " + Arrays.toString(scores)); System.out.println("Counts: " + Arrays.toString(counts)); System.out.println("Mean: " + mean); System.out.println("Mode: " + mode); } // Draw a histogram of the exam scores. // // int[] counts - the number of times each score was earned public static void drawHistogram(int[] counts) { for (int i = 0; i < counts.length; i++) { System.out.printf("%3d | ", i); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); } } // Computes and returns the number of exam scores in each of several ranges // of the given size. // // int[] scores - the individual exam scores // int bucketSize - the number of possible scores to be included in each "bucket" public static int[] getBucketedCounts(int[] scores, int bucketSize) { int[] counts = new int[MAX_SCORE / bucketSize + 1]; for (int i = 0; i < scores.length; i++) { int score = scores[i]; counts[score / bucketSize]++; } return counts; } // Draws a histogram of the exam scores with each column representing a range // of the given size. // // int[] counts - the number of times each score was earned // int bucketSize - the number of possible scores to be included in each "bucket" public static void drawBucketedHistogram(int[] counts, int bucketSize) { for (int i = 0; i < counts.length; i++) { System.out.printf("%3d | ", i * bucketSize); for (int j = 0; j < counts[i]; j++) { System.out.print("*"); } System.out.println(); } } // Prints out the given array in the following format: // [1, 2, 3, 4, 5] // // int[] nums - the array to be printed public static void printArray(int[] nums) { if (nums.length == 0) { System.out.println("[]"); } else { System.out.print("["); System.out.print(nums[0]); for (int i = 1; i < nums.length; i++) { System.out.print(", " + nums[i]); } System.out.println("]"); } } }