In: Computer Science
Homework Assignment 4 Instructions: Class name must be: HW4_yourName For example: Michael will name the class of homework assignment 4 as HW4_Michael Grading Rubric: Code running and as per the required conditions and giving expected output = 10 points File named as per instructions = 1 point Comments in code = 4 points Problem: Average calculation for a list Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1 is: 24.0 Average for student John is 26.00 Average for student Michael is 27.00 Average for student Adelle is 22.33
Note : I saved text file in D drive , you gave to specify the right path for the text file in your PC.
I have used comments so that you can get , full marks
import java.util.*;
import java.io.*;
public class HW4_yourName {
public static void main(String args[]){
//Getting file from local drive
File scores = new File("D:\\test_scores.txt");
FileReader f = new FileReader(scores);
BufferedReader br = new BufferedReader(f);
String line;
String[] test_scores;
// This while loop reads each line as a string, then split string into words to get test score details
ArrayList<String> list = new ArrayList<>();
int test1 =0,test2=0,test3=0;
int total_students = 0;
while((line = br.readLine()) != null){
//Splitting each line in file into words
test_scores = line.split(" ");
//storing marks of each test in a varibale for average
test1 += Integer.parseInt(test_scores[1]);
float average = (tInteger.parseInt(est_scores[1])+Integer.parseInt(test_scores[2])+ Integer.parseInt(test_scores[3]))/3 ;
// storing the string into the list for final printing
list.add("Average of Student "+test_scores[0] + "is" + average );
total_students+=1;
}
System.out.println("Class average for test1 is "+test1/total_students);
//Printing marks for each student
for(String s: list){
System.out.println(s);
}
}
}
HOPE i helped YOU.
Happy Learning.