Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

I need help with a class constructor that accepts an array of test scores

801497Feb 21 2009 — edited Feb 22 2009
Hello, I'm a student working on my java programming and have run into some problems. I have two files, one for the class and the other is the program to run it. Here are the instructions and my code.

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.

My problem is that I have no idea on how to setup the class file to check for a negative or number greater than 100. Please help. Thanks.

Class File
public class TestScores{

    final int ARRAY_SIZE = 5;
    
    double[] badScores = new double[ARRAY_SIZE];
    double tGood =0;
    double tBad = 0;
    double average;
    
    public TestScores(double[] badScores) {
        
 
for (int index = 0; index < badScores.length; index++)
    tGood += badScores[index];
    }
  
    public String getAverage() {
        
        return null;
    }
  }
Program file
public class TestScoresDemo {


    public static void main(String[] args) {
//      An array with test scores.
        // Notice that element 3 contains an invalid score.
        double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };

        // Another array with test scores.
        // All of these scores are good.
        double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };
        
        // Create a TestScores object initialized with badScores.
        try
        {
           TestScores tBad = new TestScores(badScores);
           // The following statement should not execute.
           System.out.println("The average of the bad scores is " +
                              tBad.getAverage());
        }
        catch (IllegalArgumentException e)
        {
           System.out.println("Invalid score found.\n" + e.getMessage());
        }
     
        // Create a TestScores object initialized with goodScores.
        try
        {
           TestScores tGood = new TestScores(goodScores);
           System.out.println("The average of the good scores is " +
                              tGood.getAverage());
        }
        catch (IllegalArgumentException e)
        {
           System.out.println("Invalid score found.\n" + e.getMessage());
        }
     }
  }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 22 2009
Added on Feb 21 2009
5 comments
1,968 views