Please read it carefully before doing the programming.!!!Attached is a class nam
ID: 3734296 • Letter: P
Question
Please read it carefully before doing the programming.!!!Attached is a class named TestScores, which stores a set of SCORES passed by the user to the class constructor, and includes a method that returns the average of the scores. Modify this class as follows:
1. In the constructor, if the ARRAY passed by the user is null or has zero length, then an IllegalArgumentException should be thrown.
2. In the getAverage method, if any of the test scores in the array is negative, then an InvalidTestScoreException should be thrown. InvalidTestScoreException is a custom exception class that you must write.
Write a demo class that does the following:
1. Asks the user to enter a filename, and tries to open it. If an exception is thrown while opening the file, it should be caught and the user should be asked to re-enter the filename. This should take place in a do-while loop. DO NOT use the exists() method for this question.
2. Once the file is open, you can assume that it contains a set of double values. These values should be read and stored in an array.
3. A TestScores object should then be instantiated using the array that was read from the user.
4. The getAverage method of the TestScores class should be used to obtain the average of the scores, and print it out to the user.
5. All exceptions that can be thrown by the modified TestScores class should be caught and handled (by gracefully terminating the program).
public class TestScores
{
private double[] array ;
public TestScores (double[] array)
{
this.array = new double [array.length] ;
for (int i = 0 ; i < array.length ; i ++)
this.array [i] = array [i] ;
}
public double getAverage ()
{
double sum = 0 ;
for (double x : array)
sum += x ;
return sum / array.length ;
}
}
Save the following scores into a badData.txt to test your program.
77.0 52.8 85.8 -80.7 76.4 87.6 94.7
84.5 84.0 75.1 88.1 75.4 97.0 72.1
Save the following scores into a goodData.txt to test your program.
77.0 52.8 85.8 80.7 76.4 87.6 94.7
84.5 84.0 75.1 88.1 75.4 97.0 72.1
Explanation / Answer
Please find my implementation:
// PROGRAM 1
//TestScores Class
class TestScores
{
int[] score_array;
// The class constructor should accept an array of test scores as its argument .
public TestScores(int[] array) throws IllegalArgumentException
{
score_array = new int[array.length];
for(int i=0; i<array.length; i++)
{
//. If any test score in the array is negative or greater than 100,
// the class should throw an IllegalArgumentException.
if(array[i]<0 || array[i]>100)
throw new IllegalArgumentException("Invalid test Score ");
else
score_array[i] = array[i];
}
}
//The class should have a method that returns the average of the scores
public double getAverage()
{
double total = 0;
for(int i=0; i<score_array.length; i++)
total = total+score_array[i];
return total/score_array.length;
}
}
// demonstrate the class in a program.
public class TestScoresMain
{
public static void main(String[] args)
{
int array[] = {89,90,91,92};
// test case 1. all scores are good.
TestScores TS1 = new TestScores(array);
// here no exception will be thrown or caught because all scores are good.
System.out.println("Test Score Average Given by :"+TS1.getAverage());
int array1[] = {89,90,91,-92};
// test case 2.
// here one score is invalid and it is less than 0 so
// it should throw InvalidTestScore Exception.
try
{
TestScores TS2 = new TestScores(array1);
}
// one InvalidTestScore Exception thrown catch and display message.
catch(IllegalArgumentException ep)
{
System.out.println("Exception thrown :" + ep);
}
}
}
// PROGRAM 2
//Write an exception class named InvalidTestScore.
class InvalidTestScore extends Exception
{
public InvalidTestScore(String message)
{
super(message);
}
}
class TestScores
{
int[] score_array;
// below constructor throws InvalidTestScore Exception if any score is invalid.
public TestScores(int[] array) throws InvalidTestScore
{
score_array = new int[array.length];
for(int i=0; i<array.length; i++)
{
// check any score is less than 0 or > 100
// if so throw InvalidTestScore Exception.
if(array[i]<0 || array[i]>100)
throw new InvalidTestScore("Invalid test Score ");
else
score_array[i] = array[i];
}
}
//The class should have a method that returns the average of the scores
public double getAverage()
{
double total = 0;
for(int i=0; i<score_array.length; i++)
total = total+score_array[i];
return total/score_array.length;
}
}
public class TestScoresMain
{
public static void main(String[] args)
{
// test case 1. all scores are good.
int array[] = {89,90,91,92};
try
{
TestScores TS1 = new TestScores(array);
System.out.println("Test Score Average Given by :"+TS1.getAverage());
}
// here no exception will be thrown or caught because all scores are good.
catch(InvalidTestScore ep)
{
System.out.println("Exception thrown :" + ep);
}
int array1[] = {89,90,91,-92};
// test case 2.
// here one score is invalid and it is less than 0 so
// it should throw InvalidTestScore Exception.
try
{
TestScores TS2 = new TestScores(array1);
}
// one InvalidTestScore Exception thrown catch and display message.
catch(InvalidTestScore ep)
{
System.out.println("Exception thrown :" + ep);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.