Im having trouble on the tally part code and assignment below. 1. Ignore the pol
ID: 3662274 • Letter: I
Question
Im having trouble on the tally part code and assignment below. 1. Ignore the poll data from anyone who selected a Saturday, a Sunday, or February 29th. 2. Ignore the poll data from anyone who selected a Friday and also wants to split the event over two days. 3. +1 point just for taking the poll. 4. +2 points if the poll data is from a current student or alumnus. 5. +3 points if the poll data is from someone who will attend the event. 6. +4 points if the poll data is from someone who selected the 26th for this event. For example, for 26:1:No:Alumnus, Student, Staff, add 7 to February 26th since 1 point just for taking the poll, 2 points for being either an alumnus or a student, and 4 points for selecting the 26th for this event. As another example, for 27:2:yes:Faculty, Alumnus, add 0 to February 27th since we’re ignoring data from anyone who selected a Saturday. A tally2 is the total number of points for each of the days in Febru- 2 tally ary 2016. Once you tally the poll data from the file, prepare a report. Your report should show: • The tally of points for the top 5 days in February and the total and average of those 5 days. • The tally of points for all 7 days of the week in February and the total and average of those 7 days. • The preferred day and day of the week for alumni and students. • The preferred day and day of the week for faculty and staff. • What percentage of poll takers will attend this event? Do not ignore any poll data for this calculation. • What percentage of poll takers want this event on the 26th? Do not ignore any data for this calculation. package jlott1; import java.io.*; import java.util.*; public class Main { // These are CONSTANTS that will not be changed once initialized. // If you change NUM_POLL_RESULTS to N, only the first N pool results // will be used. private static final String FILENAME = "poll.txt"; private static final int NUM_POLL_RESULTS = 1111; private static final ArrayList<String> CATEGORIES = new ArrayList(Arrays.asList("Student", "Alumnus", "Staff", "Faculty")); public static void main(String[] args) throws IOException { System.out.println(" Welcome to my Xavier Poll Analysis! "); // Uncomment this statement to generate a different input file. createPollResults(FILENAME); // This makes it so we can read from the input file named FILENAME Scanner infile = new Scanner(new File(FILENAME)); String result = ""; // Initialize the tallies for each day by initializing them to zero. int counts[] = new int[29];// ignore index 0 Arrays.fill(counts, 0); for (int i = 1; i <= NUM_POLL_RESULTS; i++) { result = infile.nextLine(); // read a line from the input file // chop the line into parts String[] parts = result.split(":"); int day = Integer.parseInt(parts[0]); int span = Integer.parseInt(parts[1]); String attend = parts[2]; String categories = parts[3]; if(! ignoreResult(day, span)) { // get 1 point just for taking the poll counts[day]++; // get 2 points if a student or an alumnus if(categories.contains("Student") || categories.contains ("Alumnus")) { counts[day] += 2; System.out.println(); } // get 3 points if planning to attend the investiture if(attend.equals("Yes")) { counts[day] += 3; } // get 4 points if selected February 26th if(day == 26) { counts[day] += 4; } } } // output the array of all the counts System.out.println("Tally (index i is the # of tallies for Day " + "i, ignore index 0):"); System.out.println(Arrays.toString(counts)); //sorting the array Arrays.sort(counts); //tally for top 5 int[] top5 = Arrays.copyOfRange(counts,counts.length-5,counts.length); System.out.println("Top 5 days are "+ Arrays.toString(top5)); //average of those top 5 days double average; if(top5.length % 2 == 0) average = (double)top5[top5.length/2]+ (double)top5[top5.length/2-1]/2; else average = (double) top5[top5.length/2]; System.out.println("The average of the top 5 is" +" "+ average); //tally for all 7 //pref day alumni/students //pref day faculty and staff //percentage of poll takers will attend //percentage on 26th } /** * Decide whether the current day should be ignored in the tally * @param day day of the month * @param span event is either on 1 or 2 days * @return true if the current poll result should be ignored */ private static boolean ignoreResult(int day, int span) { if(dayOfWeek(day).equals("Sun")) return true; if(dayOfWeek(day).equals("Sat")) return true; if(day == 29) return true; if(dayOfWeek(day).equals("Fri") && span==2) return true; return false; } /** * Returns the day of the week for a particular day in February 2016. * @param day day of the week ( * @return */ private static String dayOfWeek(int day) { String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // February 1st, 2016 is a Monday. return days[day%7]; } /** * Generate a new input file of NUM_POLL_RESULTS poll results. * @param filename name of the input file */ private static void createPollResults(String filename) { PrintWriter outfile = null; try { outfile = new PrintWriter(filename); } catch (FileNotFoundException e) { e.printStackTrace(); } Random rand = new Random(); int day = 0; int span = 0; String attend = ""; String categories = ""; for (int i = 1; i <= NUM_POLL_RESULTS; i++) { day = rand.nextInt(29) + 1; span = rand.nextInt(2) + 1; attend = (rand.nextBoolean()) ? "Yes" : "No"; Collections.shuffle(CATEGORIES); int numCategories = rand.nextInt(4) + 1; categories = ""; for(int j=0; j<numCategories-1; j++) { categories += CATEGORIES.get(j) + ", "; } categories += CATEGORIES.get(numCategories-1); outfile.println(day + ":" + span + ":" + attend + ":" + categories); } outfile.close(); } }
Explanation / Answer
Also change the logic in 'ignoreResult(int day, int span)' method: