TestScores Class Design a TestScores class that has fields to hold an ID and thr
ID: 3562836 • Letter: T
Question
TestScores Class
Design a TestScores class that has fields to hold an ID and 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 three instances of the class. Then the program should display the average of the scores, as shown in the following table:
ID Test1 Test2 Test3 Average
34557 70 90 66 75.33
34456 80 90 75 81.67
78979 65 65 80 70.00
Explanation / Answer
/*** This is the program Class **/
public class Test_KagawaDavid
{
private double test1, test2,test3, average;
public Test_KagawaDavid(double t1, double t2, double t3)
{
test1=t1;
test2=t2;
test3=t3;
}
//MUTATORS
public void setTest1(double t1)
{
test1=t1;
}
public void setTest2(double t2)
{
test2=t2;
}
public void setTest3(double t3)
{
test3=t3;
}
//Access
public double getTest1()
{
return test1;
}
public double getTest2()
{
return test2;
}
public double getTest3()
{
return test3;
}
public double average()
{
return((test1+test2+test3)/3);
}
}
/** This is the exe file **/
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Test_KagawaDavidDemo
{
public static void main(String [] args)
{
Test_KagawaDavid Scores = new Test_KagawaDavid(10.00,20.00,30.00);
Scanner input = new Scanner(System.in);
System.out.println("Enter Test1 ");
Scores.setTest1(input.nextDouble());
System.out.println("Enter Test2 ");
Scores.setTest2(input.nextDouble());
System.out.println("Enter Test3 ");
Scores.setTest3(input.nextDouble());
System.out.println("Average of these scores is: " + Scores.average());
JOptionPane.showMessageDialog(null,"Program Complete");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.