Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,870 people online right now. Registration is fast and FREE... Join Now!




TestScore

 
Reply to this topicStart new topic

TestScore

allican57
18 Sep, 2007 - 09:54 AM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 37


My Contributions
/*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!");
    }
}




User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: TestScore
18 Sep, 2007 - 10:08 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
CODE

if(score < 0 || score > 100)
throw new IllegalArgumentException();

it's as simple as that, but you will need to catch it properly.
User is offlineProfile CardPM
+Quote Post

allican57
RE: TestScore
18 Sep, 2007 - 10:14 AM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 37


My Contributions
QUOTE(William_Wilson @ 18 Sep, 2007 - 11:08 AM) *

CODE

if(score < 0 || score > 100)
throw new IllegalArgumentException();

it's as simple as that, but you will need to catch it properly.

Thank you much!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:08AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month