Question: Focus on merge method, if code has no compiler errors i will thumbs Boiler Plate Code. [Copy this into your project]. package com.company; import java.io.File;
Focus on merge method, if code has no compiler errors i will thumbs
![thumbs Boiler Plate Code. [Copy this into your project]. package com.company; import](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f467692179a_28866f46768922e2.jpg)

![HW4 { private static String[] namesA = null; private static double[] gradesA](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4676adad14_29066f4676a536be.jpg)
![= null; private static String[] namesB = null; private static double[] gradesB](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4676bb4f16_29166f4676b2d7f7.jpg)
Boiler Plate Code. [Copy this into your project]. package com.company; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * */ public class HW4 { private static String[] namesA = null; private static double[] gradesA = null; private static String[] namesB = null; private static double[] gradesB = null; private static String[] namesC = null; private static double[] gradesC = null; private static String[] namesAll = null; private static double[] gradesAll = null; /** * * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { PrintWriter writer = new PrintWriter("output.txt"); processSectionA(writer, "grade1.txt"); processSectionB(writer, "grade2.txt"); processSectionC(writer, "grade3.txt"); // TODO: Uncomment the below two lines of code when you reach step 8 // merge(namesA, namesB, namesC, gradesA, gradesB, gradesC); // process(writer, "Overall", namesAll, gradesAll); writer.close(); } /** * * @param writer * @param inputFileName * @throws FileNotFoundException */ public static void processSectionA(PrintWriter writer, String inputFileName) throws FileNotFoundException { File f = new File(inputFileName); Scanner reader = new Scanner(f); String section = reader.nextLine(); int size = reader.nextInt(); namesA = new String[size]; gradesA = new double[size]; initializeAndProcess(writer, reader, section, namesA, gradesA); } public static void processSectionB(PrintWriter writer, String inputFileName) throws FileNotFoundException { File f = new File(inputFileName); Scanner reader = new Scanner(f); String section = reader.nextLine(); int size = reader.nextInt(); namesB = new String[size]; gradesB = new double[size]; initializeAndProcess(writer, reader, section, namesB, gradesB); } public static void processSectionC(PrintWriter writer, String inputFileName) throws FileNotFoundException { File f = new File(inputFileName); Scanner reader = new Scanner(f); String section = reader.nextLine(); int size = reader.nextInt(); namesC = new String[size]; gradesC = new double[size]; initializeAndProcess(writer, reader, section, namesC, gradesC); } public static void initializeAndProcess(PrintWriter writer, Scanner reader, String section, String[] names, double[] grades) throws FileNotFoundException { int index = 0; while (reader.hasNext()) { names[index] = reader.next(); grades[index] = reader.nextDouble(); index++; } process(writer, section, names, grades); } /** * * @param writer * @param section * @param names * @param grades * @throws FileNotFoundException */ public static void process(PrintWriter writer, String section, String[] names, double[] grades) throws FileNotFoundException{ int minIndex = findMinIndex(grades); int maxIndex = findMaxIndex(grades); double average = findAverage(grades); flush(writer, section, minIndex, maxIndex, average, names, grades); } /** * * @param grades * @return */ public static int findMinIndex(double[] grades) { int minIndex = 0; // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE INDEX THAT HAS MINIMUM SCORE return minIndex; } /** * * @param grades * @return */ public static int findMaxIndex(double[] grades) { int maxIndex = 0; // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE INDEX THAT HAS MAXIMUM SCORE return maxIndex; } /** * * @param grades * @return */ public static double findAverage(double[] grades) { double average = 0; // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE AVERAGE SCORE return average; } /** * * @param writer * @param section * @param minIndex * @param maxIndex * @param average * @param names * @param grades * @throws FileNotFoundException */ public static void flush(PrintWriter writer, String section, int minIndex, int maxIndex, double average, String[] names, double[] grades) throws FileNotFoundException { // TODO: You will need to modify this method to also print // TODO: the additional analytical data for overall section writer.write("-------------------------------" + " "); writer.write(section + " "); writer.write("Minimum: " + names[minIndex] + " , " + String.valueOf(grades[minIndex]) + " "); writer.write("Maximum: " + names[maxIndex] + " , " + String.valueOf(grades[maxIndex]) + " "); writer.write("Average: " + String.valueOf(average) + " "); writer.write("-------------------------------" + " "); } /** * * @param namesA * @param namesB * @param namesC * @param gradesA * @param gradesB * @param gradesC */ public static void merge(String[] namesA, String[] namesB, String[] namesC, double[] gradesA, double[] gradesB, double[] gradesC) { // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE 3 NAMES ARRAY, AND THE 3 GRADES ARRAY, // TODO - MERGE THEM INTO 1 NAMES ARRAY, and 1 GRADES ARRAY. namesAll = new String[namesA.length + namesB.length + namesC.length]; gradesAll = new double[gradesA.length + gradesB.length + gradesC.length]; } }
Reading, Processing and Processing data using Arrays and Files As you know CSS 142 has multiple sections. Let's say all sections had their midterm1 and the grades were handed back to the students. You are tasked with writing a program that will give some analytics to the CSS department on how the students fared across all the three sections Further, let's assume, grades per section were provided via an input file "data1.txt" as below Section 142A John 97.5 Jim 99 Kathy 34 Steve 86.5 Stacy 43 Faith 88 You can safely assume the following 1, 2. 3. 4. File data is 100% valid First line will tell the section name Second line will tell you the number of entries (the number of students) From third line onwards, you will have a Name score pair a. b. C. d. e. There will be only 2 entries per line. Name and Score, and nothing else There will be exact 1 space between Name and Score Grades will always be a double type. Always between 0 and 100 Name will always be a String type Names, Scores, Min and Max is unique within and across sections Requirements Read this file Create two arrays 1. 2. a. First to store the Grades b. Find highest, lowest and average scores. Create a new file called "output.txt". In this file write the highest score, lowest score, average score Repeat this for all 3 sections Merge the grades for three sections Find and write the highest score, lowest score, average score across the three sections Second to store the Names 3. 4. 5. 6. 7. Reading, Processing and Processing data using Arrays and Files As you know CSS 142 has multiple sections. Let's say all sections had their midterm1 and the grades were handed back to the students. You are tasked with writing a program that will give some analytics to the CSS department on how the students fared across all the three sections Further, let's assume, grades per section were provided via an input file "data1.txt" as below Section 142A John 97.5 Jim 99 Kathy 34 Steve 86.5 Stacy 43 Faith 88 You can safely assume the following 1, 2. 3. 4. File data is 100% valid First line will tell the section name Second line will tell you the number of entries (the number of students) From third line onwards, you will have a Name score pair a. b. C. d. e. There will be only 2 entries per line. Name and Score, and nothing else There will be exact 1 space between Name and Score Grades will always be a double type. Always between 0 and 100 Name will always be a String type Names, Scores, Min and Max is unique within and across sections Requirements Read this file Create two arrays 1. 2. a. First to store the Grades b. Find highest, lowest and average scores. Create a new file called "output.txt". In this file write the highest score, lowest score, average score Repeat this for all 3 sections Merge the grades for three sections Find and write the highest score, lowest score, average score across the three sections Second to store the Names 3. 4. 5. 6. 7
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
