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

Attached is a class named TestScores, which stores a set of scores passed by the

ID: 3734108 • Letter: A

Question

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 ;

            }

}

Explanation / Answer

/**The java program that prompts user
* to enter the name of the file and
* read integers and then print
* the average of the values.*/
//TestDriver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestDriver {

   public static void main(String[] args) {

       String fileName = null;
       boolean run=true;
       Scanner filescanner=null;
       Scanner kb=new Scanner(System.in);
       double nums[];
       int size=0;
       int index=0;
       //do while loop
       //run until input file name is valid
       do
       {
           try {

               System.out.println("Enter file name :");
               fileName=kb.nextLine();
              
               filescanner=new Scanner(new File(fileName));
               run=false;

           } catch (FileNotFoundException e) {
               System.out.println(e.getMessage());
               run=true;
           }
          
       }
       while(run);
      
       //read file and count the number of elements in file
       while(filescanner.hasNextLine())
       {
           filescanner.nextDouble();
           size++;
       }
       //create array of type double
       nums=new double[size];
       filescanner.close();
      
       try {
           //create an instance of the Scanner with file name
           //as input argument
           filescanner=new Scanner(new File(fileName));
           //read elements from the file
           while(filescanner.hasNextLine() && index<size)
           {
               nums[index]=filescanner.nextDouble();
               index++;
           }
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       }
       //Create a class instance called,TestScores with nums array
       TestScores tscores=new TestScores(nums);
       //calling getAverage method
       System.out.println("Average of scores : "+tscores.getAverage());
      
   } //end of the main method
}//end of the class ,TestScores

--------------------------------------------------------------


//TestScores.java
public class TestScores
{
   private double[] array ;
   public TestScores (double[] array)
   {
       this.array = new double [array.length] ;
       //checking if array is null or length is 0
       if (array==null || array.length==0)
           throw new IllegalArgumentException();
       else
       {
           for (int i = 0 ; i < array.length ; i ++)
               this.array [i] = array [i] ;
       }
   }
   public double getAverage ()
   {
       double sum = 0 ;
       for (double x : array)
       {
          
           //implement the exception class if x is negative value
           try {
               if(x<0)
                   throw new InvalidTestScoreException("Negative score");
           } catch (InvalidTestScoreException e) {
               System.out.println(e.getMessage());
           }
           sum += x ;
       }
       return sum / array.length ;
   }
}

-------------------------------------------------

//InvalidTestScoreException.java
public class InvalidTestScoreException
extends Exception{
   public InvalidTestScoreException(String msg) {
       super(msg);
   }

}

---------------------------------------------------------------

Sample run:

badData.txt

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

Ouptut :

Enter file name :
badData.txt
Negative score
Average of scores : 69.27142857142857

goodData.txt

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

Output :

Enter file name :
goodData.txt
Average of scores : 80.8