Question: TestScores ClassWrite a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method

TestScores ClassWrite 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.b)TestScores Class Custom ExceptionWrite 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.

The Skeletal Code

public class TestScores

{

// Variable to reference an array holding test scores

private double[] scores;

/**

The constructor initializes an object with

an array of scores. If the array contains

an invalid value (less than 0 or greater than

100) an exception is thrown.

@param s The array of test scores.

@exception IllegalArgumentException When the

argument array contains an invalid

value.

*/

public TestScores(double[] s) throws IllegalArgumentException

// When throws is used, callstack is visited to identify the pre-written // catch.

{

// Create an array to hold the scores passed

// as an argument.

// Copy the scores passed as an argument into

// the new array. Check for illegal values as

// they are copied.

// add code to visit element of the array and check the validity

throw new IllegalArgumentException("Element: " + i + " Score: " + s[i]);

// add code for assigning valid element to the scores array

/**

The getAverage method returns the average

of the object's test scores.

@return The average of the object's test scores.

*/

public double getAverage()

{

// add code

}

}

/**

This program demonstrates a solution to the

TestScores Class programming challenge.

*/

public class TestScoresDemo

{

public static void main(String[] args)

{

// An array with test scores.

// Notice that element 3 contains an invalid score.

double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };

// Another array with test scores.

// All of these scores are good.

double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };

// Create a TestScores object initialized with badScores.

try

{

//add code

}

catch (// add exception name and the parameter)

{

System.out.println("Invalid score found. " + // add code);

}

// Create a TestScores object initialized with goodScores.

try

{

// add code

}

catch (// add exception name and the parameter)

{

System.out.println("Invalid score found. " + // add code);

}

}

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!