Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions