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

Challenge 1 Write a class named TestScores . The class constructor should accept

ID: 3712728 • Letter: C

Question

Challenge 1

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.

Challenge 2

Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Exercise 1 so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid.

Test your exception in a program (in a Driver class located in the same file). Your program should prompt the user to enter the number of test scores, and then ask for each test score individually. Then, it should print the average of the test scores.

1 (TestScores Class) + 2 (TestScores Class Custom Exception) + 9 (TestScores Modification for Serialization)

In Challenge 9:

Use everything from Challenge 1 and 2.

Print out the average of the test scores after deserialization

Submit the output from serialization

Explanation / Answer

Challenge 1:

The program consists of two classes- TestScoresDemo and TestScores.

TestScoresDemo.class:

import java.util.*;

import java.lang.*;

import java.io.*;

public class TestScoresDemo

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.print( "Please enter number of tests: " );

int numOfScores = keyboard.nextInt();

double[] scores = new double[numOfScores];

for( int i=0; i < numOfScores; i++ )

{

System.out.print( "Enter the score for Test " +(i+1)+ ": " );

scores[i] = keyboard.nextDouble();

}   

try

{

TestScores curScores = new TestScores(scores);

System.out.println(" Average score for test: " + curScores.getAvgScore());

}

catch (IllegalArgumentException e)

{

System.out.println(" You have entered an invalid test score. Please check your scores and run the application again. " + e.getMessage());

}

}

}

# TestScores.class:

public class TestScores
{
private double[] _scores;
private double _avg;

/**
* calls val_and_avg and stores that in the _avg field
* then if exception is not thrown stores the
* array in _scores field
   */


public TestScores(double[] scores) throws IllegalArgumentException
{
_avg = val_and_avg(scores);
_scores = scores;
}

/**
* tests if each score is within the range
* if not throws IllegalArgumentException to calling code
* then adds scores to avg, then calculates and returns
* the average score as a double

*/
private double val_and_avg(double[] scores)
{
double avg = 0;
for(int i =0; i < scores.length; i++)
{
if(scores[i] < 0 || scores[i] > 100)
throw new IllegalArgumentException("Score for test #"+ (i+1) + " is Invalid.");
avg += scores[i];
}
return (avg / scores.length);
}

/**
* getter method to be called by demo program to retrieve the avg score
*/
public double getAvgScore()
{
return _avg;
}
}

Challenge 2:

The TestScores will throw InvalidTestScore exception instead of IllegalArgumentException. The reamining code will remain same.

InvalidTestScore.class

public class InvalidTestScore extends Exception
{

public InvalidTestScore()

{

super("Error : Invalid Test Score");

}

  

public InvalidTestScore(double invalid)

{

super("Error : Invalid Test Score: " + invalid);

}


}

/* * This program uses the TestScores class * First it prompts user for number of scores to enter * creates an array of that size and then prompts * user for each score to enter into array * then gets the average of the scores by * calling the .getAvgScore method of the TestScores class */