Reputation: 9
This is an myprogramminglab question for school that I just can't get to work... It has to go on one page, is why I have it all together.
My compile error:
Driver.java:3: error: class TestScores is public, should be declared in a file named TestScores.java
public class TestScores
^
Driver.java:51: error: constructor TestScores in class TestScores cannot be applied to given types;
TestScores TestScore = new TestScores();
^
required: double[]
found: no arguments
reason: actual and formal argument lists differ in length
2 errors
The Exercise:
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 named Driver. The program should ask the user to input the number of test scores to be counted, and then each individual test score. It should then make an array of those scores, create a TestScore object , and print the average of the scores.
If an IllegalArgumentException is thrown, the main method should catch it, print "Test scores must have a value less than 100 and greater than 0." and terminate the program .
My Code:
import java.util.Scanner;
public class TestScores
{
private double[] scoreArray;
public TestScores(double[] test) throws IllegalArgumentException
{
scoreArray = new double[test.length];
for (int i = 0; i < test.length; i++)
{
if (test[i] < 0 || test[i] > 100)
throw new IllegalArgumentException("Test scores must have a value less than 100 and greater than 0.");
else
scoreArray[i] = test[i];
}
}
public double getAverage()
{
double total = 0.0;
for (int i = 0; i < scoreArray.length; i++)
total += scoreArray[i];
return (total / scoreArray.length);
}
public static void main(String[] args)
{
int score = 0;
int scores = 0;
Scanner userInput = new Scanner(System.in);
System.out.print("Enter number of test scores: ");
score = userInput.nextInt();
double[] scoreArray = new double[score];
for (int i = 0; i <= score - 1; i++)
{
System.out.print("Enter test score " + (i + 1)+ ": ");
scoreArray[scores] = userInput.nextDouble();
}
TestScores TestScore = new TestScores();
System.out.print(TestScore);
}
}
Upvotes: 0
Views: 5233
Reputation: 2453
Make sure your TestScores class is saved in a file named TestScores.java. Also, make sure to use your constructor properly. If the constructor takes an int array then pass it an int array when calling it.
TestScores testScore = new TestScores(scoreArray);
Also, by convention, variable names should start with a lowercase letter.
Upvotes: 1
Reputation: 1
Keep in mind when you are creating classes always add a No-Args contrusctor for thoses situations
Your problem is you need to provide the test array parameter that you specified in the constructor
Upvotes: 0
Reputation: 51
You have to provide to the instance:
TestScores TestScore = new TestScores(scoreArray);
because you have in the class the constructor named TestScores and it receives an array, test it and let us know !
Upvotes: 0
Reputation: 495
It could be that the constructor is being invoked first when the program starts, and there is no double there. ERROR. Then you are trying create an instance on the class without passing a double in the parameters
Upvotes: 0