// Tyler Rigsby, CSE 142 // Computes several students' section attendance scores from // poorly formatted data using array tallying import java.util.*; import java.io.*; public class SectionAttendance { // constants? public static void main(String[] args) throws FileNotFoundException { // make scanner // loop over each line (section): // get points for each // get percentages for each // report results Scanner input = new Scanner(new File("sections.txt")); int section = 0; while (input.hasNextLine()) { String line = input.nextLine(); section++; int[] studentPoints = getPoints(line); double[] studentPercentages = new double[studentPoints.length]; double average = getPercentages(studentPoints, studentPercentages); reportResults(section, studentPoints, studentPercentages, average); } } // Takes a line containing student grades as a parameter, and returns // an integer array containing the points that each student received. public static int[] getPoints(String line) { int[] counts = new int[5]; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); int points = 0; if (c == 'y') { points = 3; } else if (c == 'n') { points = 1; } int student = i % 5; counts[student] += points; if (counts[student] > 20) { counts[student] = 20; } } return counts; } // Takes an integer array containing each students' points and a double array of the same // size and fills the double array with each student's grades given 20 points possible. Returns // the average grade among all students. public static double getPercentages(int[] studentPoints, double[] percentages) { double sum = 0.0; for (int i = 0; i < percentages.length; i++) { percentages[i] = 100.0 * studentPoints[i] / 20.0; sum += percentages[i]; } return sum / percentages.length; } // Takes the section number, students' points, grades, and section average as parameters, // and prints the statistics about the section. public static void reportResults(int section, int[] points, double[] percentages, double average) { System.out.println("Section #" + section + ":"); System.out.println("Student Points: " + Arrays.toString(points)); System.out.println("Student Grades: " + Arrays.toString(percentages)); System.out.println("Section average: " + average); System.out.println(); } }