// Marty Stepp, CSE 142, Autumn 2008 // This program tallies student attendance scores for different sections. // Reports attendance, scores and grades for each student. // // This version sends its output to a file instead of to the console. import java.util.*; import java.io.*; public class Sections2 { // maximum points that a student can earn public static final int MAX = 20; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); PrintStream output = new PrintStream(new File("sections_out.txt")); while (input.hasNextLine()) { String sectionData = input.nextLine(); int[] attended = computeAttendance(sectionData); int[] scores = computeScores(attended); double[] grades = computeGrades(scores); results(attended, scores, grades, output); } } // Computes the number of sections each student attended based on the given data. // Returns the attendance data as an array. public static int[] computeAttendance(String sectionData) { int[] attended = new int[5]; for (int i = 0; i < sectionData.length(); i++) { if (sectionData.charAt(i) == '1') { // '1' or '0' attended[i % 5]++; } } return attended; } // Computes the points earned by each student based on the given attendance data. // Returns the points data as an array. public static int[] computeScores(int[] attended) { int[] scores = new int[5]; for (int i = 0; i < attended.length; i++) { scores[i] = Math.min(3 * attended[i], MAX); } return scores; } // Computes the grade percentage of each student based on the given points earned and a max score of 20. // Returns the grades data as an array. public static double[] computeGrades(int[] scores) { double[] grades = new double[5]; for (int i = 0; i < scores.length; i++) { grades[i] = 100.0 * scores[i] / MAX; } return grades; } // Prints results about this section to the given output file. public static void results(int[] attended, int[] scores, double[] grades, PrintStream output) { output.println("Sections attended: " + Arrays.toString(attended)); output.println("Student scores: " + Arrays.toString(scores)); output.println("Student grades: " + Arrays.toString(grades)); output.println(); } }