File hw6_task1.java contains an incomplete program. The goal of the program is c
ID: 3693440 • 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 (feel free to use the methods posted in class) 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:
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.
Explanation / Answer
//Assuming there is valid data
package assignment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
/**
* This program ...
* @author
*/
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) {
//read the input file
try {
Scanner sc = new Scanner(new File(input_name));
int count = 0;
String output = "Name, Avg Exam Score, Avg HomeWork Score, Exam Grade ";
String tokens[] = null;
String temp = null;
double examAvg =0;
double hwAvg = 0;
double sum = 0;
if(sc.hasNext()) {
sc.nextLine(); //row with column names
while(sc.hasNext()) {
temp = sc.nextLine();
tokens = temp.split(","); //csv files are read as columns seperated by ','
output = tokens[0]+" "+tokens[1]; //first name and last name concateneated
//get the 3 exam scores and compute avg
sum = Double.parseDouble(tokens[2])+Double.parseDouble(tokens[3])+Double.parseDouble(tokens[4]);
examAvg = sum/3.0;
output += ","+examAvg;
//get the 3 exam scores and compute avg
sum = Double.parseDouble(tokens[5])+Double.parseDouble(tokens[6])+Double.parseDouble(tokens[7]);
hwAvg = sum/3.0;
output += ","+hwAvg+" ";
}
System.out.println(output);
//write output to file saved as .csv
FileOutputStream out = new FileOutputStream(csv_name);
out.write(output.getBytes());
out.close();
//write output to file saved as .pretty file
out = new FileOutputStream(pretty_name);
out.write(output.getBytes());
out.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.