Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io . * ; import java.util.Scanner; public class StudentGradeReport { public static void main ( String [ ] args ) { Scanner scanner =

import java.io.*;
import java.util.Scanner;
public class StudentGradeReport {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
// Step 1: Read the file name of the tsv file from the user
System.out.print("Enter the name of the TSV file: ");
String tsvFileName = scanner.nextLine();
try {
// Step 2: Open the tsv file and read student information
BufferedReader tsvReader = new BufferedReader(new FileReader(tsvFileName));
FileWriter reportFile = new FileWriter("report.txt");
BufferedWriter reportWriter = new BufferedWriter(reportFile);
double totalMidterm1=0.0;
double totalMidterm2=0.0;
double totalFinal =0.0;
int studentCount =0;
String line;
while ((line = tsvReader.readLine())!= null){
String[] data = line.split("\t");
if (data.length ==5){
String lastName = data[0];
String firstName = data[1];
double midterm1= Double.parseDouble(data[2]);
double midterm2= Double.parseDouble(data[3]);
double finalScore = Double.parseDouble(data[4]);
// Calculate the average exam score
double averageScore =(midterm1+ midterm2+ finalScore)/3;
// Assign a letter grade
String letterGrade = assignLetterGrade(averageScore);
// Output student information to report.txt
reportWriter.write(lastName +"\t"+ firstName +"\t"+ midterm1+"\t"+ midterm2+"\t"+ finalScore +"\t"+ letterGrade);
reportWriter.newLine();
// Update totals for average exam scores
totalMidterm1+= midterm1;
totalMidterm2+= midterm2;
totalFinal += finalScore;
studentCount++;
}
}
tsvReader.close();
reportWriter.close();
// Calculate and output the average of each exam with two digits after the decimal point
double avgMidterm1= totalMidterm1/ studentCount;
double avgMidterm2= totalMidterm2/ studentCount;
double avgFinal = totalFinal / studentCount;
FileWriter avgFile = new FileWriter("report.txt", true);
BufferedWriter avgWriter = new BufferedWriter(avgFile);
avgWriter.newLine();
avgWriter.write("Average Exam Scores: ");
avgWriter.write(String.format("Midterm1: %.2f\tMidterm2: %.2f\tFinal: %.2f", avgMidterm1, avgMidterm2, avgFinal));
avgWriter.close();
System.out.println("Report generated successfully!");
} catch (IOException e){
System.err.println("An error occurred while processing the file: "+ e.getMessage());
}
}
private static String assignLetterGrade(double averageScore){
if (averageScore >=90){
return "A";
} else if (averageScore >=80){
return "B";
} else if (averageScore >=70){
return "C";
} else if (averageScore >=60){
return "D";
} else {
return "F";
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions