Write a program thatcan store a student’s first name, last name, five test scores,
average tests score, and grade. (Use an array to store the test scores.) The program must contain methods
to calculate test averages, return test averages, calculate grades, return grades,
and modify individual test scores. The method toString must return test data (including student’s
name, five test scores, average, and grade) as a string. Write a program to calculate students’ average
test scores and the grade. You may assume the following input data:
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
Samantha 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
The program should output data as close as possible to the following form:
First_Name Last_Name Test1 Test2 Test3 Test4 Test5 Average Grade
Jack Johnson 85.00 83.00 77.00 91.00 76.00 82.40 B
Lisa Aniston 80.00 90.00 95.00 93.00 48.00 81.20 B
Andy Cooper 78.00 81.00 11.00 90.00 73.00 66.60 D
Ravi Gupta 92.00 83.00 30.00 69.00 87.00 72.20 C
Bonny Blair 23.00 45.00 96.00 38.00 59.00 52.20 F
Danny Clark 60.00 85.00 45.00 39.00 67.00 59.20 F
Samantha Kennedy 77.00 31.00 52.00 74.00 83.00 63.40 D
Robin Bronson 93.00 94.00 89.00 77.00 97.00 90.00 A
Sheila Sunny 79.00 85.00 28.00 93.00 82.00 73.40 C
Kiran Smith 85.00 72.00 49.00 75.00 63.00 68.80 D
Class average = 70.94
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
int[] scores;
String s;
String[] data;
while (!(s = in.nextLine()).equals("")) {
scores = new int[5];
data = s.split(" ");
for (int i = 0; i < 5; i++) {
scores[i] = Integer.parseInt(data[i + 2]);
}
students.add(new Student(data[0], data[1], scores));
}
System.out.println("First_Name Last_Name Test1 Test2 Test3 Test4 Test5 Average Grade");
double classAvg = 0;
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
classAvg += students.get(i).testAverage();
}
System.out.printf("Class average = %.2f\n", classAvg / students.size());
}
}
public class Student {
private String firstName;
private String lastName;
private int[] scores;
public Student(String firstName, String lastName, int[] scores) {
this.firstName = firstName;
this.lastName = lastName;
this.scores = scores;
}
public double testAverage() {
double total = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
return total / scores.length;
}
public void setScores(int[] scores) {
this.scores = scores;
}
public char grade(double average) {
if (average >= 90) {
return 'A';
} else if (average >= 80) {
return 'B';
} else if (average >= 70) {
return 'C';
} else if (average >= 60) {
return 'D';
} else {
return 'F';
}
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(firstName).append(" ").append(lastName).append(" ");
for (int i = 0; i < scores.length; i++) {
buffer.append(scores[i]).append(" ");
}
buffer.append(String.format("%.2f", testAverage())).append(" ").append(grade(testAverage()));
return buffer.toString();
}
}
Comments
Leave a comment