/* CSE 142, Autumn 2007, Marty Stepp This program reads an input file of data about students' attendance in section and outputs the attendance, points earned, and grade percentages for each student in each section. The format of the input file is explained in the Ch. 7 lecture slides. Example line of input from the file, sections.txt: 111111101011111101001110110110110001110010100 Example output for the above line: Sections attended: [9, 6, 7, 4, 3] Student scores: [20, 18, 20, 12, 9] Student grades: [100.0, 90.0, 100.0, 60.0, 45.0] */ import java.io.*; import java.util.*; public class Sections { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sections.txt")); // process each section while (input.hasNextLine()) { String line = input.nextLine(); // count attendance for each student in this section int[] attendance = new int[5]; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c == '1') { // character at index i refers to student at index (i % 5) // i 0 1 2 3 4 5 6 7 8 9 10 // i % 5 0 1 2 3 4 0 1 2 3 4 0 attendance[i % 5]++; } } System.out.println("Sections attended: " + Arrays.toString(attendance)); // compute points earned for each student in this section int[] points = new int[5]; for (int i = 0; i < attendance.length; i++) { points[i] = Math.min(20, attendance[i] * 3); } System.out.println("Student scores: " + Arrays.toString(points)); // compute percentage earned for each student in this section doPercents(points); System.out.println(); } } // This method computes each student's percentage earned based on the // given array of points earned by each student. // This is shown as an example of passing an array as a parameter. public static void doPercents(int[] points) { double[] percent = new double[5]; for (int i = 0; i < points.length; i++) { percent[i] = 100.0 * points[i] / 20.0; } System.out.println("Student grades: " + Arrays.toString(percent)); } }