USING JAVA ! A. Create a new project named StudentScores Create a Student class
ID: 3754056 • Letter: U
Question
USING JAVA !
A. Create a new project named StudentScores
Create a Student class with instance data as follows:student id and the scores for three tests.
Provide 2 constructors as follows:
1) parameter values for all instance data fields
2) parameter for only the student id
Provide a method called setTestScore that accepts two parameters: the test number (1 through 3) and the score. You will need to use 'if' statements to set the score to the proper test.
Also provide a method getTestScore that accepts the test number and returns the appropriate score. You will need to use 'if' statements to get the proper test score.
Provide a method called average that computes and returns the average test score for this student
Create a DisplyInfo method to show student ID, test scores, and average test score.
B. Create a driver program called TestScores.
Create 2 students as follows:
ID FC123
ID FC789
Student FC123 is created with the follow three test scores: 100, 80, 94
Student FC456 is created only with the id. Later, call setTestScore with three scores 78, 92, 80
Explanation / Answer
TestScores.java
public class TestScores {
public static void main(String[] args) {
Student s1 = new Student("FC123");
Student s2 = new Student("FC789");
s1.setTestScore(1, 100);
s1.setTestScore(2, 80);
s1.setTestScore(3, 94);
s2.setTestScore(1, 78);
s2.setTestScore(2, 92);
s2.setTestScore(3, 80);
s1.DisplyInfo();
s2.DisplyInfo();
}
}
Student.java
public class Student {
private String studentId;
private int score1,score2,score3;
public Student (String studentId) {
this.studentId = studentId;
}
public Student (String studentId, int score1, int score2, int score3) {
this.studentId = studentId;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public void setTestScore(int testNo, int score) {
if(testNo==1) {
this.score1 = score;
} else if(testNo==2) {
this.score2 = score;
} else {
this.score3 = score;
}
}
public int getTestScore(int testNo) {
if(testNo==1) {
return this.score1;
} else if(testNo==2) {
return this.score2;
} else {
return this.score3;
}
}
public double average() {
return (score1+score2+score3)/(double)3;
}
public void DisplyInfo() {
System.out.println("Student Id: "+studentId+" Score1: "+score1+" Score2: "+score2+" Score3: "+score3+" Average: "+average());
}
}
Output:
Student Id: FC123 Score1: 100 Score2: 80 Score3: 94 Average: 91.33333333333333
Student Id: FC789 Score1: 78 Score2: 92 Score3: 80 Average: 83.33333333333333
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.