// Helene Martin, CSE 142 // Calculates scores for section attendance based on a data file // Structured by thinking of the high-level tasks: // tallying scores, calculating percents, reporting results // Each data transformation is a method. import java.util.*; import java.io.*; public class SectionStructured { public static void main(String[] args) throws FileNotFoundException { Scanner fScan = new Scanner(new File("sections.txt")); int section = 1; while (fScan.hasNextLine()) { String line = fScan.nextLine(); int[] scores = countScores(line); double[] percents = calcPercents(scores); report(section, scores, percents); section++; } } // given a String representing section attendance, return scores for 5 students public static int[] countScores(String line) { int[] scores = new int[5]; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (c == 'y') { // every fifth score belongs to a particular student scores[i % 5] += 3; } else if (c == 'n') { scores[i % 5]++; } // cap scores at 20; scores[i % 5] = Math.min(scores[i % 5], 20); } return scores; } // given a set of scores for 5 students, return their percentage grades public static double[] calcPercents(int[] scores) { double[] percents = new double[5]; for (int i = 0; i < scores.length; i++) { percents[i] = scores[i] / 20.0 * 100; } return percents; } public static void report(int section, int[] scores, double[] percents) { System.out.println("Section " + section); System.out.println("Student scores: " + Arrays.toString(scores)); System.out.println("Student percent: " + Arrays.toString(percents)); System.out.println(); } }