need help with illegalArgumentException

hi, every

i'm new to java.

i need u to help to grade my program:

Write a class named TestScores. The class contructor should accept an array of test scores as its argument. the class should have a method that return the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw as IllegalArgumentException.Demonstrate the class in a program.

1- Did i program right so far for my first class?

2-I dont know where to put this part :If any test score in the array is negative or greater than 100.

here is my first class:

publicclass TestScores

{

privatedouble[] testScores;

public TestScores(double[] ts)

{

testScores = ts;

}

publicdouble getAverage()

{double total =0;

double average= 0;

try

{

for (int i =0; i<testScores.length; i++)

total += testScores[i];

average = total/testScores.length;

}

catch(IllegalArgumentException e)

{

System.out.println(" Bad number format.");

}

return average;

}

>

[1904 byte] By [thecanma] at [2007-11-27 11:42:04]
# 1

>1- Did i program right so far for my first class?

Does it do what it's supposed to do? Doesn't look bad on a first glance, although I'd let the c'tor throw the exception.

> 2-I dont know where to put this part :If any test

> score in the array is negative or greater than 100.

How about placing it where you get the input...

CeciNEstPasUnProgrammeura at 2007-7-29 17:41:59 > top of Java-index,Java Essentials,New To Java...
# 2

You're close to correct:

- You are not supposed to catch an IllegalArgumentException, but you should throw one.

- Include a check inside the loop that checks for negative and too big values and if it finds one throws an IllegalArgumentException.

Good luck with that.

JoachimSauera at 2007-7-29 17:41:59 > top of Java-index,Java Essentials,New To Java...
# 3

I believe that data should be evaluated when it enters the system. Or instance, in this context. So the getAverage() method would be too late to throw the exception.

CeciNEstPasUnProgrammeura at 2007-7-29 17:41:59 > top of Java-index,Java Essentials,New To Java...