Zoom in for better quality Design a TestScores class that has fields to hold thr
ID: 3576578 • Letter: Z
Question
Zoom in for better quality
Design a TestScores class that has fields to hold three test scores. The class should have a constructor, accessor and mutator methods for the test score fields, and a method that returns the average of the test scores. Demonstrate the class by writing a separate program that creates an instance of the class. The program should ask the user to enter three test scores, which are stored in the TestScores object. Then the program should display the average of the scores, as reported by the Test Scores object.Explanation / Answer
TestScoresDriver.java
import java.util.Scanner;
public class TestScoresDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter score1: ");
int score1 = scan.nextInt();
System.out.println("Enter score2: ");
int score2 = scan.nextInt();
System.out.println("Enter score3: ");
int score3 = scan.nextInt();
TestScores t = new TestScores(score1, score2, score3);
System.out.println("Average is "+t.average());
}
}
TestScores.java
public class TestScores {
private int score1, score2, score3;
public TestScores(){
}
public TestScores(int s1,int s2, int s3){
this.score1 = s1;
this.score2 = s2;
this.score3 = s3;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
public double average(){
return (score1+ score2+score3)/3.0;
}
}
Output:
Enter score1:
77
Enter score2:
88
Enter score3:
99
Average is 88.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.