Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this the correct way to use an IllegalArgumentException?
    primarykey
    data
    text
    <p>I'm trying to work on a Java assignment. This is what it asks:</p> <blockquote> <p>Write a class named <code>TestScores</code>. The class constructor should accept an array of the test scores as its argument. The class should have a method that returns the average of the test scores. If an test score in the array is negative or greater than 100, the class should throw an <code>IllegalArgumentException</code>. Demonstrate. I need a file named <code>TestScores</code> and <code>TestScoresDemo</code>.</p> </blockquote> <p>This is what I have so far. I know some of it is wrong and I need help fixing it:</p> <pre><code>class TestScores { public static void checkscore(int s) { if (s&lt;0) throw new IllegalArgumentException("Error: score is negative."); else if (s&gt;100) throw new IllegalArgumentException("Error Score is higher then 100"); else if (s&gt;89)throw new IllegalArgumentException("Your grade is an A"); else if (s&gt;79 &amp;&amp; s&lt;90)throw new IllegalArgumentException("Your grade is an B"); else if (s&gt;69 &amp;&amp; s&lt;80)throw new IllegalArgumentException("Your grade is an C"); else if (s&gt;59 &amp;&amp; s&lt;70)throw new IllegalArgumentException("Your grade is an D"); else if (s&lt;60)throw new IllegalArgumentException("Your grade is an F"); { int sum = 0; //all elements together for (int i = 0; i &lt; a.length; i++) sum += a[i]; } return sum / a.length; } } class TestScoresDemo { public static void main(String[] args) { int score = 0; Scanner scanner = new Scanner(System.in); System.out.print(" Enter a Grade number: "); String input = scanner.nextLine(); score = Integer.parseInt(input); TestScores.checkscore(score); System.out.print("Test score average is" + sum); } } </code></pre> <p>I know the assignment calls for a <code>try</code> statement because in my book that's what I see with the <code>IllegalArgumentException</code>. Can anyone help me? I'm using Eclipse as an IDE.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload