In: Computer Science
Java program Test Scores?
Design a TestScore class that has three integer fields, each
holding a test score. The
class should have accessor and mutator methods for the test score
fields and a method that
returns the average of the test scores as a double.
Test the TestScore 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 should be stored in the TestScore object.
The program should then print the average of the scores.
CODE FOR TestScore CLASS:(TestScore.java)
class TestScore
{
public int test_score1;
public int test_score2;
public int test_score3;
public TestScore(int t1,int t2,int t3)
{
test_score1 = t1;
test_score2 = t2;
test_score3 = t3;
}
public int getScore1()
{
return test_score1;
}
public void setScore1(int test_score1)
{
this.test_score1 = test_score1;
}
public int getScore2()
{
return test_score2;
}
public void setScore2(int test_score2)
{
this.test_score2 = test_score2;
}
public int getscore3()
{
return test_score3;
}
public void setScore3(int test_score3)
{
this.test_score3 = test_score3;
}
public double average()
{
double avg = (test_score1+test_score2+test_score3)/3;
return avg;
}
}
CODE FOR AvgScore CLASS:(AvgScore.java)
import java.util.Scanner;
public class AvgScore
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three test scores: ");
int score1 = sc.nextInt();
int score2 = sc.nextInt();
int score3 = sc.nextInt();
TestScore cs = new TestScore(score1,score2,score3);
double avrg = cs.average();
System.out.println("The average of three test scores
is:"+avrg);
}
}
OUTPUT: