hi. i need help for a problem.
i have an array of double values in the main method. i am trying to create an object and pass the array to a different class using an overloaded constructor.
i am having trouble with the syntax. please help, i need to use try/catch here to test values and then get average.
so far in main i have:
and in the class Scores, this is what have so far, still not sure how to access the array elements from main:public class TestScores { public static void main(String[] args) { double[] scores1 = {55.5,60.5,70.5,130.0,80.0}; // THIS SHOULD CAUSE EXCEPTION IN Scores class double[] scores2 = {55.5,60.5,70.5,100.0,80.0}; Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY?? Scores gs = new Scores(double[] goodScores); // HERE ALSO. System.out.println("Average: " + bs.getAverage()); } }
public class Scores { private double[] badScores; private double[] goodScores; public double average; public String str; public int LCV; public void Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN??? { badScores = bad; goodScores = good; } public void TestInvalid() { try { if (badScores[0] > 0 || badScores[0] < 100) { average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5; } else if (goodScores[0] > 0 || goodScores[0] < 100) { average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5; } } catch (IllegalArgumentException ex) { str = "Invalid score found"; } } public double getAverage() { return average; } public String getInvalid() { return str; } }