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

Write a class named TestScores. The class constructor should accept an array of

ID: 3574892 • Letter: W

Question

Write a class named TestScores. The class constructor should accept an array of test scores as an 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 IIlegalArgumentException. Demonstrate the class in a test program. Write an exception class named InvalidTestScoreException. Modify the TestScores class you wrote previously so that it throws an InvalidTestScoreException if any of the test score in the array are invalid (negative or greater than 100). Demonstrate the class in a test program.

Explanation / Answer

package snippet;

public class TestScores {
private int[] a;
TestScores()
{
   a=new int[100];
}

TestScores(int array[])
{
   a=array;
}

float avarage()
{
   float sum=0;
   for(int i=0;i<a.length;i++)
   {
      
              
               sum+=a[i];
      
      
      
   }
   return sum/a.length;
  
}
   public static void main(String[] args) {
       int a[]={10,20,30,40,50,60};
       try
       {
           for(int i=0;i<a.length;i++)
           {
               if(a[i]>100 ||a[i]<0)
               {
                  

                   throw new IllegalArgumentException();
               }
                  
           }
           TestScores o=new TestScores(a);
           System.out.println("Average is "+o.avarage());
          
       }
       catch(IllegalArgumentException e)
       {
           System.out.println("IllegalArgumentException");
          
       }
      
      

   }

}

=================================================================================

Output:

Average is 35.0

=================================================================================

package snippet;

public class TestScores {
private int[] a;
TestScores()
{
   a=new int[100];
}

TestScores(int array[])
{
   a=array;
}

float avarage()
{
   float sum=0;
   for(int i=0;i<a.length;i++)
   {
      
              
               sum+=a[i];
      
      
      
   }
   return sum/a.length;
  
}
   public static void main(String[] args) throws InvalidTestScoreException {
       int a[]={10,20,30,40,-50,60};
       try
       {
           for(int i=0;i<a.length;i++)
           {
               if(a[i]>100 ||a[i]<0)
               {
                  

                   throw new InvalidTestScoreException();
               }
                  
           }
           TestScores o=new TestScores(a);
           System.out.println("Average is "+o.avarage());
          
       }
       catch(InvalidTestScoreException e)
       {
           System.out.println("InvalidTestScoreException");
          
       }
      
      

   }

}

==========================================================================================

package snippet;

public class InvalidTestScoreException extends Exception
{
   InvalidTestScoreException()
   {
       super();
   }

}

=====================================================================================

Outptut:

InvalidTestScoreException

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote