Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please support with comments in the project In your assignment,explain your code

ID: 3844545 • Letter: P

Question

Please support with comments in the project

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.txt

Explanation / Answer

Hi There!

I have heavily commented the code of the program so you can understand each and every step clearly.

Also I have tested it with a dummy scores.txt file. Please give a Thumbs up if you like the answer.

//Code starts here.

import java.io.*;
import java.util.ArrayList;
import java.util.List;

class Grades {

//Creating variables to store data of each student in Grades Object.
//Data will be read from input file.
//storing marks of qui in an ArrayList.
private int studentNo;
private List<Integer> quiz = new ArrayList<>();

public static void main(String[] args){

//Taking file name from command line argument
String inputFileName = args[0];

try{ //Try block to handle IOException which is thrown in case of Input/Output error.

//Declaring and Instantiating FileReader and BufferedReader Objects to read data from input file.
FileReader fr = new FileReader(inputFileName);
BufferedReader reader = new BufferedReader(fr);

//Declaring and Instantiating FileWrite and BufferedWriter Objectes to write data to output file.
FileWriter fw = new FileWriter("finalReport.txt");
BufferedWriter writer = new BufferedWriter(fw);

/* Reading First line from input file. First line will contain number of students
* and number of quizzes. We are using split method in string to split these two
* numbers. Therefore lengthOfData[0] will give number of students and
* lengthOfData[1] will give number of quiz.
*/
String lengthOfData[] = reader.readLine().split(" ");
int noOfStudents = Integer.parseInt(lengthOfData[0]);
int noOfQuizzes = Integer.parseInt(lengthOfData[1]);

//Now creating an array of grades to hold all students data
Grades gradesList[] = new Grades[noOfStudents];

//Loop for reading data of all students
for (int i = 0; i < noOfStudents; i++) {

gradesList[i] = new Grades();
//Read a line and split the data delimited by space
//First index will carry student id number
//rest will have quiz marks of a student
String studentData[] = reader.readLine().split(" ");

//Store student number in its object
gradesList[i].studentNo = Integer.parseInt(studentData[0]);

//Loop through the quiz marks and store quiz marks in student object
for (int j = 0; j < noOfQuizzes; j++) {
gradesList[i].quiz.add(Integer.parseInt(studentData[j+1]));
}
}
//Now we all the data stored in gradesList array
//Now we will write it into our output file

//First Line. Writing all headings
writer.write("Student No");
for (int i = 0; i < noOfQuizzes; i++) {
writer.write(" Quiz" + (i+1));
}
writer.write(" Average");
writer.newLine();

//Second Line. Writing dashes.
writer.write("----------");
for (int i = 0; i < noOfQuizzes; i++) {
writer.write("-------------");
}
writer.write("------------------");
writer.newLine();

//Now writing data of students line by line
for (int i = 0; i < noOfStudents; i++) {
writer.write(String.valueOf(gradesList[i].studentNo));
writer.write(" ");

int sum = 0;
for (int j = 0; j < noOfQuizzes; j++) {
sum += gradesList[i].quiz.get(j);
  
//String.valueOf() of converts Integer to string
//get method of list returns item at position j
  
writer.write(String.valueOf(gradesList[i].quiz.get(j)));
writer.write(" ");
}
float average = sum / noOfQuizzes;
writer.write(String.format("%.1f", average));
writer.newLine();
}

//Writing dashes.
writer.write("----------");
for (int i = 0; i < noOfQuizzes; i++) {
writer.write("-------------");
}
writer.write("------------------");
writer.newLine();
writer.write(" ");

//Now finding quiz averages and writing last line.

float quizAverages[] = new float[noOfQuizzes];

for (int i = 0; i < noOfQuizzes; i++) {
int sum = 0;
for (int j = 0; j < noOfStudents; j++) {
sum += gradesList[j].quiz.get(i);
}
float average = sum / noOfStudents;
quizAverages[i] = average;
  
//String.format is used to show average upto only 1 decimal places.
writer.write(String.format("%.1f", average));
writer.write(" ");
}

  
float sum = 0;
for (int i = 0; i < noOfQuizzes; i++) {
sum += quizAverages[i];
}
float avQuizAverages = sum / noOfQuizzes;
writer.write(String.format("%.1f", avQuizAverages));

//Closing input/output streams
  
reader.close();
writer.close();

}catch (IOException e){ //Catch IOException
e.printStackTrace(); // and print stack trace.
}


}
}

//Code ends here

Hope it helps!