I have written the code for the following problem and would just like some feedback before I move on. The problem states:
Write a class names TestScores. The class constructor should accept an array of test scores as its arguement. 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 IllegalArguementException. Demonstrate the class in a program.
Here is my code:
import java.io.*;
import java.util.Scanner;
public class TestScores
{
public static void main(String[] args) throws IOException
{
try
{
Scanner keyboard = new Scanner(System.in);
System.out.print( "Please enter number of tests: " );
int numOfTests = keyboard.nextInt();
double sumOfAllGrades = 0;//the sumOfAllGrades
for( int i=0; i < numOfTests; i++ )
{
System.out.print( "Enter the grade for Test " +(i+1)+ ": " );
double grade = keyboard.nextDouble();
sumOfAllGrades += grade;
}
double average = sumOfAllGrades/numOfTests;
System.out.println("The average is: " + average);
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
Does this seem to meet the requirements? Thanks for your help!