// Reads an input file containing attendance information and reporting for each // student their attendance for each day of the week and calculating which day's // lectures were attended most frequently. import java.util.*; import java.io.*; public class AttendanceFinal { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("attendance.txt")); // each line has meaning (each line is a different student) int studentCount = 0; while (input.hasNextLine()) { String attendanceLine = input.nextLine(); studentCount++; // process entire line / entire attendance data for one student // count attendance on each day (process String from input file) /* we have two options regarding weekdayCount array: 1) can create the array here (where we call the counting method) and pass the array reference as a parameter 2) the counting method creates array AND returns it below we show option 2 */ int[] weekdayCount = countAttendance(attendanceLine); // figure out which day was attended most often String mostAttendedDay = findMostAttendedDay(weekdayCount); // String mostAttendedDay; // if (maxIndex == 0) { // mostAttendedDay = "Mon"; // } else if (maxIndex == 1) { // mostAttendedDay = "Tues"; // } else if (maxIndex == 2) { // mostAttendedDay = "Wed"; // } else if (maxIndex == 3) { // mostAttendedDay = "Thurs"; // } else { // mostAttendedDay = "Fri"; // } // print results printResults(studentCount, weekdayCount, mostAttendedDay); } } // Uses the given line of attendance information to tally up the number // of times each weekday was attended and returns an array of these counts // with Monday attendance at index 0, Tuesday attendance at index 1, etc. // String attendanceLine: the String containing attendance information // data transformation: // attendance string --> array of counts for each weekday public static int[] countAttendance(String attendanceLine) { int[] weekdayCount = new int[5]; for (int i = 0; i < attendanceLine.length(); i++) { // String traversal char next = attendanceLine.charAt(i); if (next == 'y') { // increment attendance for one of the days weekdayCount[i % 5]++; } } return weekdayCount; } // Uses the given count of weekday attendance and returns a String of the weekday // that had the highest attendance. // int[] weekdayCount: the array containing the number of times the student // attended each weekday lecture // data transformation: // array of counts for each weekday --> weekday with highest attendance public static String findMostAttendedDay(int[] weekdayCount) { int maxIndex = 0; for (int i = 1; i < weekdayCount.length; i++) { int nextCount = weekdayCount[i]; int prevMax = weekdayCount[maxIndex]; if (nextCount > prevMax) { // remember, i corresponds to which day we're looking at maxIndex = i; } } // "look up" array to look up which day an index corresponds to String[] weekdayLookUp = {"Mon", "Tues", "Wed", "Thurs", "Fri"}; String mostAttendedDay = weekdayLookUp[maxIndex]; return mostAttendedDay; } // Prints to the console the number of lectures the given student attended on each // weekday, and reports the day that was attended most often. // int studentCount: which student the report is for // int[] weekdayCount: the array containing the number of times the student // attended each weekday lecture // mostAttendedDay: the weekday that had the highest attendance public static void printResults(int studentCount, int[] weekdayCount, String mostAttendedDay) { System.out.println("Student #" + studentCount + "'s attendance data:"); System.out.println(" M T W Th F"); System.out.println(" " + Arrays.toString(weekdayCount)); System.out.println(" Attended " + mostAttendedDay + " lectures most often."); System.out.println(); } }