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: 3534292 • 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 .

Demonstrate the class in a program.

TestScores Class Custom Exception

Write an exception class named InvalidTestScore . Modify the TestScores class so that it throws an InvalidTestScore exception.

Modify the TestScores class in the Exceptions lab to be serializable.

Write a program that creates an array of at least five TestScore objects and serializes them.

Write another program that deserializes the objects from the file.

Explanation / Answer


public class TestScores
{
private static final int MAX_SCORE = 100;

private int[] scores;

public TestScores(int[] scores)
{
this.scores = scores;
}

private int computeAverage() throws IllegalArgumentException
{
int sum = 0;
for (int i = 0; i < scores.length; i++)
{
int score = scores[i];
if (score < 0 || score > MAX_SCORE)
{
throw new IllegalArgumentException("Score (" + score + ") is not in the range 0-" + MAX_SCORE);
}
sum += scores[i];
}
int average = sum / scores.length;
return average;
}

public static void main(String[] args)
{
TestScores testScores = new TestScores(new int[] { 50, 70, 81 });
try
{
int averageScore = testScores.computeAverage();

char letterGrade;
if (averageScore < 60) letterGrade = 'F';
else if (averageScore < 70) letterGrade = 'D';
else if (averageScore < 80) letterGrade = 'C';
else if (averageScore < 90) letterGrade = 'B';
else letterGrade = 'A';

System.out.println(" Your average score is " + averageScore + " and your grade is an " +
letterGrade);
}
catch (IllegalArgumentException illegalArgumentException)
{
System.out.println(illegalArgumentException.getMes sage());
}

}
}

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