/*Please help. I'm trying to throw an IllegalArgumentException.
*If any test score in the array is negative or greater than 100
*using a try block, but am having little if any progress after many days of work.
* I am still getting an error mesage saying not a statement on line 74 and ";"expected on the same line
*The program will compute the average and display an Error invalid Score!message for an average over 100
and will compute a negative average and display an Error invalid Score!message for an average below 0 , but
It is not doing this using the try block. The way I am interpreting the instructions is that the try block is
to make this happen, if any testScore is entered that is neg. or > 100 before the average is calculated.
Would someone please make the corrections, so that I can see what I'm missing. I have read several tutorials
and can't find the answer. I have tore this down and built it back one line at a time.
*Instructions:
Write a class named TestScores.
The class constructor should accept an array of test scores as its 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 IllegalArgumentException.
Demonstrate the class in a program.
*/
CODE
import java.io.*; // For File class and FileNotFoundException
import java.util.Scanner;// For Keyboard input
public class TestScores {
public static void main(String[] args){
//Variable Declarations
Scanner keyboard = new Scanner(System.in);
char testScore=userInput;
int testScore1=0;
int testScore2=0;
int testScore3=0;
int averageScore;
int userInput;
//User Prompts
System.out.print("Please enter your 1st test score: ");
testScore1 = keyboard.nextInt();
System.out.print("Please enter your 2nd test score: ");
testScore2 = keyboard.nextInt();
System.out.print("Please enter your 3rd test score: ");
testScore3 = keyboard.nextInt();
//Array to hold user input
char[] userInput = new char[10];
char max = 100;
//To calculate Average Score
averageScore = (testScore1 + testScore2 + testScore3)/3;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Try to assign a negative number to the testScore field.
try
{
char [] = new char[10] ("testScore "-1);
}
catch(IllegalArgumentException e)
{
System.out.println(IllegalArgumentException);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//To display average score and letter grade
if( testScore < 0)
System.out.println("\nError invalid Score!" + "invalid Score!");
else if (averageScore < 60)
System.out.println("\nYour average score is " + averageScore + " and your grade is an F");
else if(averageScore < 70)
System.out.println("\nYour average score is " + averageScore + " and your grade is an D");
else if(averageScore < 80)
System.out.println("\nYour average score is " + averageScore + " and your grade is an C");
else if(averageScore < 90)
System.out.println("\nYour average score is " + averageScore + " and your grade is an B");
else if(averageScore < 100)
System.out.println("\nYour average score is " + averageScore +" and your grade is an A");
else if( testScore > 100)
System.out.println("\nError invalid Score!" + "invalid Score!");
}
}