// Helene Martin, CSE 142 // This program takes in a file of section attendance scores and reports grades // for each student in each section. // In the data file: // -'y' represents 3 points (student went to section and turned in HW) // -'n' represents 1 point (student went to section but didn't turn in HW) // -'a' represents 0 points (student was absent) // Each student can get up to 20 section points. import java.util.*; import java.io.*; public class Section { public static final int SECTION_MAX = 20; public static void main(String[] args) throws FileNotFoundException { Scanner fileScan = new Scanner(new File("sections.txt")); PrintStream output = new PrintStream(new File("sections_out.txt")); int section = 1; while (fileScan.hasNextLine()) { String line = fileScan.nextLine(); int[] scores = getScores(line); double[] grades = getGrades(scores); printResults(output, section, scores, grades); // printResults(System.out, section, scores, grades); // to System.out section++; } } // Computes and returns scores for a given section. public static int[] getScores(String line) { int[] scores = new int[5]; for (int i = 0; i < line.length(); i++) { char current = line.charAt(i); int student = i % 5; if (current == 'y') { // did HW scores[student] += 3; } else if (current == 'n') { // didn't do HW scores[student] += 1; } scores[student] = Math.min(scores[student], SECTION_MAX); } return scores; } // Computes and returns grades out of 100 from the given scores. public static double[] getGrades(int[] scores) { double[] grades = new double[5]; for (int i = 0; i < scores.length; i++) { grades[i] = 100 * scores[i] / (double)SECTION_MAX; } return grades; } // Reports scores and grades to the specified PrintStream. public static void printResults(PrintStream out, int section, int[] scores, double[] grades) { out.println("Section " + section); out.println(Arrays.toString(scores)); out.println(Arrays.toString(grades)); out.println(); } }