In: Computer Science
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.
Use TestScoresDemo.java to test your code
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
{
TestScores tBad = new TestScores(badScores);
// The following statement should not execute.
System.out.println("The average of the bad scores is " +
tBad.getAverage());
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid score found.\n" + e.getMessage());
}
// Create a TestScores object initialized with goodScores.
try
{
TestScores tGood = new TestScores(goodScores);
System.out.println("The average of the good scores is " +
tGood.getAverage());
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid score found.\n" + e.getMessage());
}
}
}
Below is the class TestScores screenshotAnd Below is the output of the program:
And below this i am giving the whole program to be executed with all the required classes, if you face any difficulty executing the program please leave a comment :)
import java.lang.IllegalArgumentException;
class TestScores{
double scores[]; //Scores array to calculate the average if its a
valid score array
TestScores(double[] scores) {
for(int i=0; i<scores.length; i++){
try{
if (scores[i]<0)
throw new IllegalArgumentException("score must be a positive
number");
else if(scores[i]>100)
throw new IllegalArgumentException("score must be not be greater
than 100");
//Storing it after checking the conditions to save memory space by
unnecessary assignment of array if not being used
this.scores = scores; //this.scores points to the datamember of the
class and scores points to the local array obtained as parameter to
the constructor
}catch(IllegalArgumentException e){
throw e; //re throwing the exception which is catched by the catch
method in the main function
}
}
}
double getAverage(){
double sum = 0;
for(int i=0; i<this.scores.length; i++){ //this.scores.length
gives the size of the array
sum += this.scores[i];
}
return (sum/this.scores.length); //To compute the average we divide
the sum with total numbers of elements present in the array
}
}
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{
TestScores tBad = new TestScores(badScores);
// The following statement should not execute.
System.out.println("The average of the bad scores is " +
tBad.getAverage());
}catch (IllegalArgumentException e) {
System.out.println("Invalid score found.\n" +
e.getMessage());
}
// Create a TestScores object initialized with goodScores.
try{
TestScores tGood = new TestScores(goodScores);
System.out.println("The average of the good scores is " +
tGood.getAverage());
}catch (IllegalArgumentException e) {
System.out.println("Invalid score found.\n" +
e.getMessage());
}
}
}