In your assignment,explain your codes with comments. Without comments,your assig
ID: 3844028 • Letter: I
Question
In your assignment,explain your codes with comments. Without comments,your assignment will not be marked. Define all of your class member variables private explicitly and handle possible exceptions Question 1 Write a program that calculates student grades. Your program should read the data from a file that generated previously. You are asked to grade scores for 35 students. Student scores and student numbers are saved in a text file,named "scores.txt". In this file, the first line has two numbers, 35 and 5. The first number gives you number of students, and the second number gives you number of exams From the second line on, every lines have student number and scores for related exams. You can imagine the scores as Table 1. Table 1 Student Grades Student NO. Quiz1 Quiz Quiz3 Quiz4 Quiz5 12008 976 90 20 60 90 100 3002 3145 100 77 99 78 50 12555211 100 100 90 78 90 13456211 90 80 87 78 90 We want to calculates average scores of each student and calculate averages of each quiz. Your program should read the data from the scores. txt" file. When you run your program, you should have an out put as below. Also save the out put data in a file named final Report.txtExplanation / Answer
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class FinalReport {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File("scores.txt"));
PrintWriter printWriter = new PrintWriter(new File(
"finalReport.txt"));
int numberOfStudents = scanner.nextInt();
int numberOfExams = scanner.nextInt();
int[][] data = new int[numberOfStudents][numberOfExams + 1];
double[] averages = new double[numberOfExams];
System.out
.println("Student No Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 Average");
printWriter
.write("Student No Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 Average ");
System.out
.println("---------------------------------------------------------------");
printWriter
.write("--------------------------------------------------------------- ");
for (int i = 0; scanner.hasNext(); i++) {
double sum = 0;
data[i][0] = scanner.nextInt();
System.out.print(data[i][0] + " ");
printWriter.write(data[i][0] + " ");
for (int j = 1; j < data[i].length; j++) {
data[i][j] = scanner.nextInt();
System.out.print(data[i][j] + " ");
printWriter.write(data[i][j] + " ");
sum += data[i][j];
}
System.out.printf("%.2f ", (double) (sum / numberOfExams));
printWriter.write((double) (sum / numberOfExams) + " ");
}
for (int i = 0; i < numberOfExams; i++) {
averages[i] = 0;
for (int j = 0; j < numberOfStudents; j++) {
averages[i] += data[j][i + 1];
}
}
System.out
.println("---------------------------------------------------------------");
printWriter
.write("--------------------------------------------------------------- ");
System.out.print(" ");
printWriter.write(" ");
for (int i = 0; i < averages.length; i++) {
System.out.printf("%.2f ",
(double) (averages[i] / numberOfStudents));
printWriter.write((double) (averages[i] / numberOfStudents)
+ " ");
}
printWriter.flush();
printWriter.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
scores.txt
4 5
12008976 90 20 60 54 65
12008976 32 65 76 65 77
12008976 54 46 43 43 67
12008976 67 44 44 65 54
finalReport.txt:
Student No Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 Average
---------------------------------------------------------------
12008976 90 20 60 54 65 57.8
12008976 32 65 76 65 77 63.0
12008976 54 46 43 43 67 50.6
12008976 67 44 44 65 54 54.8
---------------------------------------------------------------
60.75 43.75 55.75 56.75 65.75
OUTPUT:
Student No Quiz1 Quiz2 Quiz3 Quiz4 Quiz5 Average
---------------------------------------------------------------
12008976 90 20 60 54 65 57.80
12008976 32 65 76 65 77 63.00
12008976 54 46 43 43 67 50.60
12008976 67 44 44 65 54 54.80
---------------------------------------------------------------
60.75 43.75 55.75 56.75 65.75