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

Write a class named TestScores. The class constructor should accept an array of

ID: 3547426 • Letter: W

Question

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.

And here is the criteria...

No user input is necessary

TestScoresDemo will contain arrays like

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

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

Expected output

  Bad scores

Invalid score found.

Element: 3 Score: 101.0

  Good scores

The average of the good scores is 90.24

Also

TestScoresDemo should contain the try and catch

If an error occurs the catch will display a user friendly message using System.out.println

The TestScores constructor will test each value in the array and

Explanation / Answer

public class TestScores
{
private double[] scores;
double total = 0;
// Constructor
public TestScores(double array[])
{
for (int i = 0; i < array.length; i++)
{
scores = new double[array.length];
// Validate the test scores
if (array[i] < 0 || array[i] > 100)
{
// throw an IllegalArgumentException
throw new IllegalArgumentException("Bad scores"
+ " Invalid score found."
+ " Element: " + i + " Score: " + array[i]);
}
else
{
scores[i] = array[i];
total += array[i];
}
}
}
// This methods calculates the Average of the good scores
public double getAverage()
{
return total / scores.length;
}
}

public class TestScoresDemo
{
public static void main(String[] args) throws Exception
{
double[] badScores = { 97.5, 66.7, 88.0, 10.0, 99.0 };
double[] goodScores = { 97.5, 66.7, -88.0, 100.0, 99.0 }; // Good scores is actually bad
//Create a TestScores Object
TestScores test=null;
// Try to see if goodScores or badScores will work
try
{
test = new TestScores(badScores);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
try
{
test = new TestScores(goodScores);
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
// Set and call the toString method to getAverage
// TestScores avg = new TestScores(); // ?? I'm lost here. this should return the average of the bad scores because they are valid
System.out.print("Good scores"+ " The average of the good scores is " + test.getAverage());
}
}

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