In: Computer Science
Having a difficult time writing this code up. ( JAVA based ) Will Copy and paste the whole solution I was left with. Thank you in advance !
Lab 5 – Class Statistics
Write a program which will uses the file Lab5Data.txt containing student names and the points they had earned at the end of the class. The program should use one or more arrays to create a report with the following information:
-A table containing the student name, total points they earned and the letter grade that would be given.
-The student with the highest score and the average points earned and average letter grade given
You may use single single dimension arrays, multi-dimension arrays or both.
Capture screen output of your report and save as GradeReport.txt
(Optionally, you may add code to create this file using java; see note below)
Name your program Grade.java. Upload files Grade.java, and GradeReport.txt to Canvas.
Note: the data files mentioned downloaded from the Canvas files area.
Refer to the files TipsForLab5.pdf, ReadData.txt, WriteData.txt for additional information that may be usefull in completing this lab.
The required code is given below in case of any doubts you can ask me in comments and please do upvote.
Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws FileNotFoundException
{
Scanner read = new Scanner(new File("Grades.txt"));
int numberOfStudents = 0;
while (read.hasNextLine()) {
numberOfStudents += 1;
read.nextLine();
}
read.close();
Grade[] students = new Grade[numberOfStudents];
read = new Scanner(new File("Grades.txt"));
for (int i = 0; i < students.length; ++i) {
String line = read.nextLine();
if (line == null || line.isEmpty()) continue;
String[] tokens = line.split("\\s+");
double[] scores = new double[5];
for (int j = 0; j < 5; ++j) scores[j] =
Double.parseDouble(tokens[2 + j]);
students[i] = new Grade(tokens[0], tokens[1], scores);
}
read.close();
System.out.format("%s\t\t%5s\t%5s\t%5s\t%5s\t%5s\t%5s\t%5s\n",
"Name", "Test1", "Test2", "Test3", "Test4", "Test5",
"Total", "Grade");
for (int i = 0; i < students.length; ++i) {
System.out.println(students[i]);
}
System.out.println("Class average = " +
Grade.getClassAverage(students));
}
}
Grade.java
import java.util.Arrays;
public class Grade {
private String firstName, lastName;
private double testScores[] = new double[5];
public Grade() {}
public Grade(String f, String l, double t[]) {
firstName = f;
lastName = l;
System.arraycopy(t, 0, testScores, 0, 5);
}
public Grade(Grade gradebook) {
firstName = gradebook.getFirstName();
lastName = gradebook.getLastName();
System.arraycopy(gradebook.getTestScores(), 0, testScores, 0,
5);
}
public static double getClassAverage(Grade[] students) {
if (students == null || students.length == 0) throw new
IllegalArgumentException();
double sum = 0.0;
for (int i = 0; i < students.length; ++i) {
sum += students[i].getAverage();
}
return sum / students.length;
}
public double getAverage() {
double avg = 0;
for (int i = 0; i < 5; i++) {
avg += testScores[i];
}
avg /= 5;
return avg;
}
public double getTotal() {
double tot = 0;
for (int i = 0; i < 5; i++) {
tot += testScores[i];
}
return tot;
}
public String getGrade() {
double avg = getAverage();
if (avg >= 90) {
return "A";
} else if (avg >= 80) {
return "B";
} else if (avg >= 70) {
return "C";
} else if (avg >= 60) {
return "D";
} else {
return "F";
}
}
public Grade getCopy() {
return new Grade(this);
}
public boolean isEqual(Grade gradebook) {
return
firstName.toLowerCase().equals(gradebook.getFirstName())
&&
lastName.toLowerCase().equals(gradebook.getLastName())
&& Arrays.equals(testScores,
gradebook.getTestScores());
}
public String toString() {
return String.format("%s
%s\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%3.2f\t%5.2f\t%s\n",
firstName,
lastName,
testScores[0],
testScores[1],
testScores[2],
testScores[3],
testScores[4],
getTotal(),
getGrade());
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double[] getTestScores() {
return testScores;
}
public void setTestScores(double[] testScores) {
this.testScores = testScores;
}
}
Grades.txt
Jack Johnson 85 83 77 91 76
Lisa Aniston 80 90 95 93 48
Andy Cooper 78 81 11 90 73
Ravi Gupta 92 83 30 69 87
Bonny Blair 23 45 96 38 59
Danny Clark 60 85 45 39 67
Saman Kennedy 77 31 52 74 83
Robin Bronson 93 94 89 77 97
Sheila Sunny 79 85 28 93 82
Kiran Smith 85 72 49 75 63
OUTPUT