Question: Recall the TestScores class from Chapter 11: Exceptions and I/O Lab exercise. Write a custom exception class named InvalidTestScore. Modify the TestScores class so that

Recall the TestScores class from Chapter 11: Exceptions and I/O Lab exercise.

Write a custom exception class named InvalidTestScore. Modify the TestScores class so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid, i.e., if any of the test score in the array is negative or greater than 100.

heres my program:

import java.util.Scanner; public class TestScores { int testScores[]; public TestScores(int[] testScores) throws IllegalArgumentException { for(int i=0;i { //If any of input value contain less than zero //or greater than 100 throw IllegalArgumentException if(testScores[i]<0 || testscores[i]>100) { throw new IllegalArgumentException(); } } //If everything went fine then assign testScores to class testScores variable this.testScores = testScores; } public double averageTestScore() { int sum =0; double average; for(int i=0;i { sum = sum + testScores[i]; } average =(double)sum/testScores.length; return average; } } class Driver { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int totalTestScores; try { System.out.print("Enter total number of Test Scores: "); totalTestScores =sc.nextInt(); int[] testScores = new int[totalTestScores]; for (int i = 0; i < testScores.length; i++) { System.out.print("Please enter Test Scores: "); testScores[i] = sc.nextInt(); } TestScores testScoresObj = new TestScores(testScores); System.out.println(testScoresObj.averageTestScore());

} catch (IllegalArgumentException e) { System.out.println("Test scores must have a value less than 100 and greater than 0."); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!