Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TestScores Class. Write a class named TestScores. The class constructor should a

ID: 3810914 • Letter: T

Question

TestScores Class. 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. 2. TestScores Class Custom Exception Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in problem 1 so that is throws an InvalidTestScore exception if any of the test scores in the array are invalid.

Explanation / Answer

TestScores.java

package test;

public class TestScores {
   double []testScores;

   public TestScores() {
       super();
   }

   public TestScores(double[] testScores) {
       super();
       this.testScores = testScores;
   }

   public double average(long[] scores) throws IllegalArgumentException, InvalidTestScore{
       double averageOfScore = 0;
       double value = 0;
       for(double values : scores){
           if(values > 0 && values < 100){
               int len = scores.length;
               value = value + values;
           averageOfScore = value/len;
           }
           else{
               throw new InvalidTestScore("Value is either negative or greater than 100");
           }
       }
       return averageOfScore;
   }
   public static void main(String[] args) throws IllegalArgumentException, InvalidTestScore {
       long []scores = {20,10,30,20};
       TestScores testScores = new TestScores();
       System.out.println(testScores.average(scores));
   }
  
}

InvalidTestScore.java

package test;

public class InvalidTestScore extends Exception{
   InvalidTestScore(String str){
       super(str);
   }
}

Output : when passing values 20,10,30,20

20.0

Output : when passing values 20,10,30,-5

Exception in thread "main" test.InvalidTestScore: Value is either negative or greater than 100
   at test.TestScores.average(TestScores.java:25)
   at test.TestScores.main(TestScores.java:33)

Output : when passing values 20,10,30,101

Exception in thread "main" test.InvalidTestScore: Value is either negative or greater than 100
   at test.TestScores.average(TestScores.java:25)
   at test.TestScores.main(TestScores.java:33)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote