File hw6_task1.java contains an incomplete program. The goal of the program is c
ID: 3692905 • Letter: F
Question
File hw6_task1.java contains an incomplete program. The goal of the program is compute the semester grade for each student in a class, based on exam and homework scores for that student. The grades are stored in a CSV file, following this format:
The top row contains column names. After that, each row contains data for a specific student. Column 0 contains first names. Column 1 contains last names. Columns 2, 3, 4 contain exam scores. Columns 5 to 11 contain homework scores. An example of such a CSV file is data1.txt. Complete that program, by writing methods to read the data from the file and process it. In order to process the data, define a processGrades function, that satisfies the following specs:The function takes three arguments, called input_name, csv_name, pretty_name (the filename includes the extension). Argument input_name is the name of the CSV file containing the grade data that your code needs to process, e.g. "data1.txt". Argument csv_name is the name of an output CSV file, where your code will save the grade data that you have computed. This output CSV file will have a top row with column names, and then one row per student. The file will have five columns: Column 0 contains the name of the student (concatenation of first name and last name, separated by space). Column 1 contains the average exam score for that student. Column 2 contains the average homework score for that student. Column 3 contains the smallest value between the exam average score and the homework average score for that student. Column 4 contains the letter grade, by converting the value at column 3, using standard threshold (90 and above is A, 80 to 90 is B, 70 to 80 is C, 60 to 70 is D, below 60 is F). An example output CSV file following these specifications is csv1.txt. Argument pretty_name is the name of an output text file, where your code will print out the grade data that you have computed, in a nice, tabular format that is easy for humans to read. This output text file will have a top row with column names, and then one row per student. The file will have the same five columns as the output CSV file described above. However, each column will be aligned. To obtain aligned columns, my solution uses this code to print each row: out.printf("%20s: %10.2f, %8.2f, %9.2f, %s ", name, exam_score, hw_score, min_score, grade); An example output CSV file following these specifications is pretty1.txt. For example, suppose that: You use this data1.txt as your input file. You enter "csv1.txt" for the CSV output file, and "pretty1.txt" for the pretty-print file. Then, your code should produce files that look EXACTLY like files csv1.txt and pretty1.txt obtained using my solution. Use the same format, column names and spacing as you see in these example output files. IMPORTANT: you are only allowed to modify the provided code by writing the requested functions, and possibly additional auxiliary functions (you are encouraged to come up with several such auxiliary functions in this task). You are NOT allowed to modify in any way the main function, or any other function that is already provided.
public class hw6_task1 { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter the name of the input file: ");
String inputName = in.next();
System.out.printf("Please enter the name of the output CSV file: ");
String csvName = in.next();
System.out.printf("Please enter the name of the output pretty-print file: ");
String prettyName = in.next();
processGrades(inputName, csvName, prettyName);
System.out.printf(" Exiting... "); }
public static void processGrades(String input_name, String csv_name, String pretty_name) {
}
}
Explanation / Answer
hw1task.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author prmsh
*/
public class hw6_task1 {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.printf("Please enter the name of the input file: ");
String inputName = in.next();
System.out.printf("Please enter the name of the output CSV file: ");
String csvName = in.next();
System.out.printf("Please enter the name of the output pretty-print file: ");
String prettyName = in.next();
processGrades(inputName, csvName, prettyName);
System.out.printf(" Exiting... ");
}
public static void processGrades(String input_name, String csv_name, String pretty_name) throws FileNotFoundException, IOException {
ArrayList<StudentMarks> students = new ArrayList<StudentMarks>();
BufferedReader br = new BufferedReader(new FileReader(csv_name));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
StudentMarks obj = new StudentMarks(values[0],values[1],values[2],values[3],values[4],values[5],values[6],values[7],values[8],values[9],values[10]);
students.add(obj);
}
br.close();
//writing results to file
FileWriter writer = new FileWriter(pretty_name);
for(int i=0;i<students.size();i++){
writer.append(students.get(i).getName());
writer.append(',');
writer.append(""+students.get(i).getMax());
writer.append(',');
writer.append(""+students.get(i).getMin());
writer.append(',');
writer.append(""+students.get(i).getAverage());
writer.append(' ');
}
}
}
--------------------------------------------------------------------------------------------------------------------------
StudentMarks.java
import java.util.ArrayList;
/**
*
* @author prmsh
*/
public class StudentMarks {
//name and arrays declared
String name;
ArrayList<Double> scores = new ArrayList<Double>();
public StudentMarks(String name, String marks1,String marks2,String marks3,String marks4,String marks5,String marks6,String marks7,String marks8,String marks9,String marks10){
this.name = name;
scores.add(Double.parseDouble(marks1));
scores.add(Double.parseDouble(marks2));
scores.add(Double.parseDouble(marks3));
scores.add(Double.parseDouble(marks4));
scores.add(Double.parseDouble(marks5));
scores.add(Double.parseDouble(marks6));
scores.add(Double.parseDouble(marks7));
scores.add(Double.parseDouble(marks8));
scores.add(Double.parseDouble(marks9));
scores.add(Double.parseDouble(marks10));
}
//max score
public double getMax(){
double max = 0;
for(int i=0;i<scores.size();i++){
if(scores.get(i) > max){
max = scores.get(i);
}
}
return max;
}
//min score
public double getMin(){
double min = 99999;
for(int i=0;i<scores.size();i++){
if(scores.get(i) < min){
min = scores.get(i);
}
}
return min;
}
//get average
public double getAverage(){
double total = 0;
for(int i=0;i<scores.size();i++){
total = total + scores.get(i);
}
return total / scores.size();
}
//name getter
public String getName(){
return name;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.