0

I am trying to write an exception to throw, for an invalid testScore array. If the scores are greater than 100 or less than one. I attached the three classes I have created. I also have to return the average.... If anyone can help I would appreciate it. I am getting an error in the demo class.

Attachments
public class IllegalArgumentException extends Exception
{
    public IllegalArgumentException()
    {
        super("Invalid entry");
    }
    

}
import java.io.IOException;
import java.util.Scanner;


public class TestScores
{
    double average;

    TestScores(double[] badScores)
    {
       final int numTests = 4;
        double [] testScores = new double[numTests];
        double total = 0;
        
        for (int index =0; index < testScores.length; index++)
            total += testScores[index];
            average = total /testScores.length; 
    }

     
    public void setAverage(double av)
    {
        average = av;
    
    }
        
    public double getAverage()
    {
        return average;
        
    }
    }
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());
      }
   }
}
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Ezzaral
0

If anyone can help I would appreciate it. I am getting an error in the demo class.

isn't that the whole point of throwing Exceptions? :)
could you paste the code here, instead of attaching the files? It'll make it a lot easier for us to read it

0

Yes, I know that but what I meant is that the demo class does not run... Im not sure as to what I did wrong...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.