write the definition of the class Tests such that an object of this class can st
ID: 3788867 • Letter: W
Question
write the definition of the class Tests such that an object of this class can store a student's 1st name, last name, 5 test scores, average tests score, and grade. (use array to store test scores). Add constructors and methods to manipulate data stored in an object. Among other things, your class must contain methods to calculate grades, and modify, individualing test scores. The method toString must return test data (including student's name, 5 test scores, average, and grade) as a string.
now I need to 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 Samatha Kennedy 77 31 52 74 83 Robin Bronson 93 94 89 77 97 SheilaSunny 79 85 28 93 82
Kiran Smith 85 72 49 75 63 Use an array of objects of the class Tests(designed in part a) to score each student's data. 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 77.00 82.40 B and so on with the rest of studes
class average = 70.94
this is from the Java programming from prolem analysis to program design BY: D.S. Malik
So im trying to get this in JAVA code Please.
thank you so much for anything
Explanation / Answer
TestsDriver.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class TestsDriver {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter File name :");
String fileName = scan.next();
File file = new File(fileName);
if(file.exists()){
BufferedReader br = new BufferedReader(new FileReader(file));
String sCurrentLine;
String values[];
int scores[] = new int[5];
while ((sCurrentLine = br.readLine()) != null) {
values = sCurrentLine.split(" ");
scores[0] = Integer.parseInt(values[2]);
scores[1] = Integer.parseInt(values[3]);
scores[2] = Integer.parseInt(values[4]);
scores[3] = Integer.parseInt(values[5]);
scores[4] = Integer.parseInt(values[6]);
Tests t = new Tests(values[0], values[1]);
t.setScores(scores);
System.out.println(t.toString());
}
}
else{
System.out.println("File does not exist");
}
}
}
Tests.java
public class Tests {
private String firstName;
private String lastName;
private int scores[];
private double average;
private char grade;
public Tests(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
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 int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
public double getAverage() {
double total = 0;
for(int i=0; i<scores.length; i++){
total = total + scores[i];
}
return total/scores.length;
}
public void setAverage(double average) {
this.average = average;
}
public char getGrade() {
char grades[] = {'A', 'B', 'C', 'D', 'E'};
if(getAverage() >= 90){
grade = grades[0];
}
else if(getAverage() >= 80 && getAverage() <90){
grade = grades[1];
}
else if(getAverage() >= 70 && getAverage() <80){
grade = grades[2];
}
else if(getAverage() >= 60 && getAverage() <70){
grade = grades[3];
}
else if(getAverage() >= 0 && getAverage() < 60){
grade = grades[4];
}
return grade;
}
public String toString(){
return "Name : "+getFirstName()+" "+getLastName()+" Scores : "+scores[0]+" "+scores[1]+" "+scores[2]+" "+scores[3]+" "+scores[4]+" Average Score : "+getAverage()+" Grade : "+getGrade();
}
}
Output:
Enter File name :
D:\inputdata.txt
Name : Jack Johnson Scores : 85 83 77 91 76 Average Score : 82.4 Grade : B
Name : lisa Aniston Scores : 80 90 95 93 48 Average Score : 81.2 Grade : B
Name : andy cooper Scores : 78 81 11 90 73 Average Score : 66.6 Grade : D
Name : Ravi Gupta Scores : 92 83 30 69 87 Average Score : 72.2 Grade : C
Name : Bonny Blair Scores : 23 45 96 38 59 Average Score : 52.2 Grade : E
Name : Danny Clark Scores : 60 85 45 39 67 Average Score : 59.2 Grade : E
Name : Samatha Kennedy Scores : 77 31 52 74 83 Average Score : 63.4 Grade : D
Name : Robin Bronson Scores : 93 94 89 77 97 Average Score : 90.0 Grade : A
Name : Sheila Sunny Scores : 79 85 28 93 82 Average Score : 73.4 Grade : C
Name : Kiran Smith Scores : 85 72 49 75 63 Average Score : 68.8 Grade : D
inputdata.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
Samatha 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.