/* Jessica Miller, CSE 142, Autumn 2010 This program processes section attendance information that is stored in a text file. It counts the number of attendance points for five students in three different sections and reports their grades. */ import java.io.*; import java.util.*; public class Sections { public static void main(String[] args) throws FileNotFoundException { // Section 1 // Student points: [20, 17, 19, 16, 13] // Student grades: [100.0, 85.0, 95.0, 80.0, 65.0] // box 012340123401234012340123401234012340123401234 // index 012345678901234567890123456789012345678901234 // line = "yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna" Scanner input = new Scanner(new File("sections.txt")); int sectionNumber = 1; while (input.hasNextLine()) { String section = input.nextLine(); int[] points = processSection(section); double[] grades = calculateGrades(points); reportResults(sectionNumber, points, grades); sectionNumber++; } } public static int[] processSection(String section) { int[] points = new int[5]; for (int i = 0; i < section.length(); i++) { char c = section.charAt(i); if (c == 'y') { // give student 3 points points[i % 5] = Math.min(points[i % 5] + 3, 20); } else if (c == 'n') { // give student 2 points points[i % 5] = Math.min(points[i % 5] + 2, 20); } } return points; } public static double[] calculateGrades(int[] points) { double[] grades = new double[5]; for (int i = 0; i < points.length; i++) { grades[i] = points[i] * 100.0 / 20; } return grades; } public static void reportResults(int sectionNumber, int[] points, double[] grades) { System.out.println("Section " + sectionNumber); System.out.println("Student points: " + Arrays.toString(points)); System.out.println("Student grades: " + Arrays.toString(grades)); } }