// Marty Stepp, CSE 142, Autumn 2008 // This program tallies student attendance scores for different sections. // Reports attendance, scores and grades for each student. import java.util.*; import java.io.*; public class Sections { // 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")); while (input.hasNextLine()) { // "111111101011111101001110110110110001110010100" String sectionData = input.nextLine(); // Sections attended: [9, 6, 7, 4, 3] int[] attended = computeAttendance(sectionData); // Student scores: [20, 18, 20, 12, 9] int[] scores = computeScores(attended); // Student grades: [100.0, 90.0, 100.0, 60.0, 45.0] computeGrades(scores); System.out.println(); } } // 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]++; } } System.out.println("Sections attended: " + Arrays.toString(attended)); 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); } System.out.println("Student scores: " + Arrays.toString(scores)); return scores; } // Computes the grade percentage of each student based on the given points earned and a max score of 20. public static void computeGrades(int[] scores) { double[] grades = new double[5]; for (int i = 0; i < scores.length; i++) { grades[i] = 100.0 * scores[i] / MAX; } System.out.println("Student grades: " + Arrays.toString(grades)); } }