/**Write a class named TestScores. The class constructor should
* accept an array of test scores as its argument. The class should
* have a method that returns the average of the test scores. If any
* test score in the array is negative or greater than 100, the class
* should throw an IllegalArgumentException. Demonstrate the class in a program.
*/
public class TestScores {
private double testScores[];
ScoresDemo TD = new ScoresDemo();
public TestScores(double scores[]) {
testScores = scores;
try {
for(int i = 0; i < testScores.length; i++) {
if(scores[i] < 0 || scores[i] > 100) {
throw new IllegalArgumentException("Test scores must be between 0 and 100");
}
else {
testScores[i] = scores[i];
}
}
}catch(IllegalFormatException ex) {
System.out.println(ex);
}
}
public double averageScores() {
double average = 0;
int count = testScores.length;
int sum = 0;
for(int i = 0; i < testScores.length; i++) {
sum += testScores[i];
}
average = sum / count;
return average;
}
}
public class ScoresDemo {
public static void main(String[] args) {
double testScores[] = new double [5];
TestScores scores = new TestScores(testScores);
Scanner scan = new Scanner(System.in);
for(int i = 0; i < testScores.length; i++) {
System.out.println("Enter some test scores: ");
testScores[i] = scan.nextDouble();
}
System.out.println("The average of the test scores is " + scores.averageScores());
}
}