Question: Reading and Processing data using Class Arrays and Files JAVA Lets assume, grades were provided via an input file data.txt as below. (plz note this
Reading and Processing data using Class Arrays and Files JAVA
Lets assume, grades were provided via an input file data.txt as below. (plz note this input file does not have section name.) 6 John 97.5 Jim 99 Kathy 34 Steve 86.5 Stacy 43 Faith 88 You can safely assume the following: 1. File data is 100% valid. 2. First line will tell you the number of entries (the number of students) 3. From second line onwards, you will have a Name score pair. a. There will be only 2 entries per line. Name and Score, and nothing else b. There will be exact 1 space between Name and Score c. Grades will always be a double type. Always between 0 and 100 d. Name will always be a String type. e. Names, Scores, Min and Max is unique within and across sections Requirements: 1. Read this file. 2. Create a StudentGrade arrays whose size is same as is specified in the file that is read. 3. Translate every line into a StudentGrade object and add it to the array. 4. Process this array to find the student with highest, lowest and average scores. 5. Create a new file called output.txt. In this file write the highest score, lowest score, average score. Approach: 1. Create new project 2. Create a file inside this project. Call it StudentGrade.java
3. Replace the entire contents of the file with the code snippet below marked under StudentGrade.java 4. Create a second file inside this project. Call it HW6.java 5. Replace the entire contents of the file with the code snippet below marked under HW6.java 6. Copy the sample file grade.txt below (after the code snippet) into the root folder. 7. Compile the code. Ensure the code compiles successfully. 8. Implement initialize method. 9. Implement findAverage, findMinimum and findMaximum methods. 10. Implement flush method 11. Implement any necessary constructor and getters/setters in the StudentGrades class
Boiler Plate Code. [Copy this into your project]. StudentGrade.java package com.company; public class StudentGrade { private String name; private double grade; // TODO PLEASE ADD CONSTUCTOR AND GETTERS/SETTERS } HW6.java package com.company; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * */ public class HW6 { public static void main(String[] args) throws FileNotFoundException { StudentGrade[] studentGradesA = initialize("grade.txt"); process(studentGradesA); } public static StudentGrade[] initialize(String inputFileName) throws FileNotFoundException { // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - READ THE PASSED FILE // TODO - CREATE STUDENTGRADES ARRAY // TODO FILL THIS STUDENTGRADES ARRAY WITH STUDENT OBJECTS // TODO RETURN THIS STUDENTGRADES ARRAY TO THE CALLER. return null; } public static void process(StudentGrade[] studentGrades) throws FileNotFoundException{ // Get the student with min grade StudentGrade studentGradeMin = findMinIndex(studentGrades); // Get the student with max grade StudentGrade studentGradeMax = findMaxIndex(studentGrades); // get the average grade double average = findAverage(studentGrades); // flush these details to the file flush(studentGradeMin, studentGradeMax, average, "output.txt"); } public static StudentGrade findMinIndex(StudentGrade[] studentGrades) { // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE STUDENT THAT HAS MINIMUM SCORE return null; } public static StudentGrade findMaxIndex(StudentGrade[] studentGrades) { // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE STUDENT THAT HAS MAXIMUM SCORE return null; } public static double findAverage(StudentGrade[] studentGrades) { double average = 0; // TODO - YOU NEED TO IMPLEMENT THIS METHOD // TODO - GIVEN THE ARRAY, RETURN THE AVERAGE SCORE return average; } public static void flush(StudentGrade studentGradeMinimum, StudentGrade studentGradeMaximum, double average, String outputFileName) throws FileNotFoundException { // TODO: You will need to modify this method to also print // TODO: the additional analytical data for overall section PrintWriter writer = new PrintWriter(outputFileName); writer.write("-------------------------------" + " "); writer.write("Minimum: " + "WRITE THE MIN STUDENT DETAILS" + " "); writer.write("Maximum: " + "WRITE THE MAX STUDENT DETAILS" + " "); writer.write("Average: " + String.valueOf(average) + " "); writer.write("-------------------------------" + " "); writer.close(); } } Sample Input file. [Please test your code with your own input files] 6 John 97.5 Jim 99 Kathy 34 Steve 86.5 Stacy 43 Faith 88
Expected Output file. [For the given above input file] ------------------------------- Highest: Jim, 99 Lowest: Kathy, 34 Average Score: 74.67 -------------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
