Question: Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Programming Challenge 1 so that it throws an InvalidTestScore exception if any

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Programming Challenge 1 so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid.
Please write the code as simple as possible with non-advanced techniques. Please leave detailed comments for everything. Thank you!!
Here is my code for Program Challenge 1-
public class TestScores
{
//Create score array
private double score[];
//Constructor to initialize values
public TestScores(double scores[])
{
this.score=scores;
}
//Method to calculate average
public double avg()
{
double sum =0;
for(int x=0; x=0 && score[x]<=100)
{
sum+=score[x];
}
else
{
//throw IllegalArgumentExeption
throw new IllegalArgumentException();
}
}
//Get average from sum and return avg
double avg = sum/score.length;
return avg;
}
}
public class TestScores_DEMO
{
public static void main(String[] args)
{
//Array a initialized with test scores
double a[]={10,89,75,60};
//Object 1 created and array a is passed
TestScores obj1= new TestScores(a);
//Average method called for obj1
System.out.println("average score for obj1:"+obj1.avg());
//Array b initialized with bad values
double b[]={65,10,101,-10};
//Object2 created and array b is passed
TestScores obj2= new TestScores(b);
//Average method called for obj2
System.out.println("average score for obj2:"+obj2.avg());
}
}

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!