I need help with this question in Java Write a class named TestScores. The class
ID: 3705333 • Letter: I
Question
I need help with this question in Java
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.
TestScores Class Custom Exception 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.
Explanation / Answer
Java program: Challeng 1:
public class TestScores
{
//integer array to hold test scores
int scores[];
// constructor to accept test scores
public TestScores(int[] scores)
{
this.scores = scores;
}
public double average()
{
//variable to hold sum
double sum = 0;
//loop to calculate the sum of test scores
for (int i = 0; i < scores.length; i++)
{
//condition to check test score value is either negative or >100
if (scores[i] < 0 || scores[i] > 100)
{
//throw IllegalArgumentException if invalid test score
throw new IllegalArgumentException();
}else
{
//else sum up the test scores and assign total sum to sum variable
sum = sum + scores[i];
}
}
//returns the average of the test scores
return (double) (sum / scores.length);
}
//main method to test the class object
public static void main(String[] arg)
{
//TestScore object creating with test score values
TestScores testScores = new TestScores(new int[] { 80, 73, 74, 28, 34 });
//calling the average method of TestScore class and displaying average
System.out.println("Average of Test Scores is:" + testScores.average());
}
}
Output:
Average of Test Scores is:57.8
Java program: Challeng 2:
public class TestScores
{
//integer array to hold test scores
int scores[];
// constructor to accept test scores
public TestScores(int[] scores)
{
this.scores = scores;
}
public double average() throws InvalidTestScore
{
//variable to hold sum
double sum = 0;
//loop to calculate the sum of test scores
for (int i = 0; i < scores.length; i++)
{
//condition to check test score value is either negative or >100
if (scores[i] < 0 || scores[i] > 100)
{
//throw InvalidTestScore if invalid test score
throw new InvalidTestScore("Invalid Test Score");
}else
{
//else sum up the test scores and assign total sum to sum variable
sum = sum + scores[i];
}
}
//returns the average of the test scores
return (double) (sum / scores.length);
}
//main method to test the class object
public static void main(String[] arg) throws InvalidTestScore
{
//TestScore object creating with test score values
TestScores testScores = new TestScores(new int[] {-80, 73, 74, 28, 34 });
//calling the average method of TestScore class and displaying average
System.out.println("Average of Test Scores is:" + testScores.average());
}
}
CustomException class:
public class InvalidTestScore extends Exception
{
//parametrized constructor
InvalidTestScore(String s)
{
super(s);
}
}
Output:
Exception in thread "main" ExceptionPack.InvalidTestScore: Invalid Test Score
at ExceptionPack.TestScores.average(TestScores.java:26)
at ExceptionPack.TestScores.main(TestScores.java:44)