// Zorah Fung, 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 5 points (student went to section and turned in HW) // -'n' represents 0 points (student was absent) // Each student can get up to 30 section points. import java.util.*; import java.io.*; public class Section { public static final int SECTION_MAX = 30; public static void main(String[] args) throws FileNotFoundException { Scanner fileScan = new Scanner(new File("sections.txt")); PrintStream output = System.out;// new PrintStream(new File("sections_out.txt")); int section = 1; while (fileScan.hasNextLine()) { String line = fileScan.nextLine(); // Two options: we can either // 1. Initialize student points in main and pass it to calculateStudentPoints and have // the method modify it through reference semantics (aka, an array would not need // to be returned) // 2. have calculateStudentPoints return a new result array to main. // We chose option 2 in this case. int[] studentPoints = calculateStudentPoints(line); double[] studentGrades = calculateStudentGrades(studentPoints); printResults(output, section, studentPoints, studentGrades); // printResults(System.out, section, studentPoints, studentGrades); // print to console section++; } } // Reads in a line representing attendance for a particular section and returns // an array representing the total number of points received by each student in the class // Note: We've pulled out this code into a method, because it performs a single // data transformation, converting a String of attendance for students to an array of counts public static int[] calculateStudentPoints(String line) { int[] studentPoints = new int[5]; for (int i = 0; i < line.length(); i++) { char attendance = line.charAt(i); int studentIndex = i % 5; if (attendance == 'y'){ // did the homework studentPoints[studentIndex] += 5; studentPoints[studentIndex] = Math.min(SECTION_MAX, studentPoints[studentIndex]); } /* else { studentPoints[studentIndex] += 0; }*/ } return studentPoints; } // Takes an array of points for students and returns a new array of grades out of 100 from // the given scores. public static double[] calculateStudentGrades(int[] studentPoints) { double[] studentGrades = new double[5]; for (int i = 0; i < studentGrades.length; i++) { studentGrades[i] = (double) studentPoints[i] / SECTION_MAX * 100; } return studentGrades; } // Prints the points and grades of the students in the given section, printing to the // given PrintStream public static void printResults(PrintStream output, int section, int[] studentPoints, double[] studentGrades) { output.println("Section " + section); output.println("Student points: " + Arrays.toString(studentPoints)); output.println("Student grades: " + Arrays.toString(studentGrades)); output.println(); } }