/*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 9 hours of work.
* I am still getting an error mesage saying illegal start of expression and ) expected on line 48.
*The program will compute the average and display an Error invalid Score!message for average over 100
* but only calculates the - average without the Error invalid Score! message.
*Please help to make this work.
*
*
*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.

*/

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);
        int TestScore =0;
        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
            int [0,1,2]TestScores = new int TestScores [0,1,2(testScore1,testScore2,testScore3)]];           
            int max = 100 ;
            
            
            
            
                /**
            * Constructor
                 */

            public TestScores(int testScore) {
    
            if ( testScore < 0 || testScore > 100)
                 testScore = userInput;
            else
            TestScore = testScore ;
    }

            
            
          // Try to assign a negative number to the TestScores field.
              try
              {
              TestScores  = new int ("testScores ",-1);
              
              catch (IllegalArgumentException e)
              }
              
                    finally
                    {
                        
             System.out.println(IllegalArgumentException);
          
            //To calculate Average Score
            averageScore = (testScore1 + testScore2 + testScore3)/3;
             
                
            //To display average score and letter grade
             if(testScore < 0) 
              System.out.println("\nError 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 (testScore > 100)
               System.out.println("\nError invalid Score!");
}
}
}

Recommended Answers

All 2 Replies

Well there is much more what Curtis can do...
For start he should check on unclosed bracklets for example try/catch/finaly, re-think position of public TestScores(int testScore) etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.