Question
Need assistance to modularize my code below (the client class) They should be the following methods: readInput() //let the user enter the scores.txt file, and
Need assistance to modularize my code below (the client class)
They should be the following methods:
readInput() //let the user enter the "scores.txt" file, and display the contents of the file to the screen
calculateAverageScore() //calculate the average score of all 10 records
calculateLowestScore() //calculates lowest score out of all records
calculateHighestScore() //calculates highest score out of all records
outputToCSV() //outputs the average, lowest score, highest score and all records to the CSV file
Here is the file records Below "scores.txt":
zoro 10 amara 20 latte 30 baku 49 hima 62 ola 41 naruto 9 sasuke 16 ben 10 kevin 11
Client Class Below
/*Purpose: reads inputFile in this case its scores.txt then it reads the file and calculates the averages, minimumand maximum scores. It will then make a output file "output.csv" that displays these calculations */
package lab7question2;
import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;
public class Lab7Question2 {
public static void main(String[] args) { String inputFile; String name; int score; int i = 0; int maxScore; int minScore;
double sum = 0; double average;
Score scoreArray[] = new Score[10]; //array object
final String outputFile = "batman.csv"; Scanner kb = new Scanner(System.in); System.out.print("Enter input file name: "); inputFile = kb.next();
Scanner input = new Scanner(System.in);
try { input = new Scanner(new File(inputFile)); //think of this like a new object while (input.hasNext()) // returns true if this scanner has another token in its input. Tokens in java include identifiers, keywords, literals, operators and, separators. { name = input.next(); score = input.nextInt();
scoreArray[i] = new Score(name, score); i++; } maxScore = minScore = scoreArray[0].getScore(); for (int j = 0; j < scoreArray.length; j++) { //in the for loop we are comparing each element to find the maxScore and minScore sum += scoreArray[j].getScore();
if (maxScore < scoreArray[j].getScore()) { maxScore = scoreArray[j].getScore(); }
if (minScore > scoreArray[j].getScore()) { minScore = scoreArray[j].getScore(); } }
average = sum / i; FileWriter myWriter = new FileWriter(outputFile); //Java FileWriter class is used to write character-oriented data to a file myWriter.write(i + "," + average + "," + maxScore + "," + minScore + " ");
for (int j = 0; j < scoreArray.length; j++) { myWriter.write(scoreArray[j].getName() + "," + scoreArray[j].getScore() + " "); } //closing files myWriter.close(); input.close(); System.out.println("task has been performed successfully"); } catch (IOException fileNotFound) { //handling the exception System.out.println(inputFile + " was not found. Please try again."); //input file which is the *.txt file is not in the program folder. } }
}
Server Class:
package lab7question2;
public class Score {
private String name; private int score;
public Score(String name, int score) { this.name = name; this.score = score; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getScore() { return score; }
public void setScore(int score) { this.score = score; }
}
Thanks for the Help !
More Additional Information below about the question:
The program should ask the user for the name of an input file (scores.txt). The input file should contain 10 records. Each record in the input file should consist of a name and score between 0 and 100 (inclusive). Eg: fred 95 The program should open the input file as a text file and read the contents line by line. Store each record in an array of a user-defined Score class. The Score class has 2 instance variables, a default constructor, get and set methods for the 2 instance variables.
Now process the array of Score class objects to determine:
The average of all scores
The largest score
The smallest score Now output the above stats and all records to the file output.csv.
The first line written to this file should consist of the number of records, the average score, the largest score and the smallest score; these values should be comma separated. Finally, the contents of the array of Score objects should be written to output.csv starting from the second line (the first line being taken by the above stats). The format to output the array contents is the same as the input file, with the exception that the fields should be comma separated instead of space separated.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started