HELP. Passing Array to class.
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:
Code Java:
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());
}
}
and in the class Scores, this is what have so far, still not sure how to access the array elements from main:
Code Java:
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;
}
}
Re: HELP. Passing Array to class.
Code Java:
public void Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN???
{
badScores = bad;
goodScores = good;
}
get rid of the void and you now have a constructor of class Scores that takes arrays as parameters.
Re: HELP. Passing Array to class.
Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY??
Scores gs = new Scores(double[] goodScores); // HERE ALSO.
first of all, you need both arrays since you passed them both in constructor.
Second of all, you'd pass it either
Scores gs = new Scores(scores1, scores2);
or
Scores gs = new Scores(scores2, scores1);
If you want to pass an individual array,
you're going to have to make two more constructor
public Scores (double[] goodScores)
{
}
public Scores( double[] badScores)
{
}
Re: HELP. Passing Array to class.
And you pass them the arrays you made in your main method, not the variable names from your other class.
Also, to pass an array, all you need to call that method is the array variable name you're passing it.
gs = new Scores(scores1, scores2);
Code java:
public class Scores
{
private double[] badScores;
private double[] goodScores;
public double average;
public String str;
public int LCV;
public 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;
}
public double[] getBadScores()
{
return badScores;
}
public double[] getGoodScores()
{
return goodScores;
}
}
Code java:
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 scores = new Scores(scores1, scores2); // or (scores2, scores1) not sure which
// 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());
}
}
Re: HELP. Passing Array to class.
thank you. i removed void and passed both arrays through as one object. it works now. thanks.
Re: HELP. Passing Array to class.
Quote:
Code Java:
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;
}
is the code really calculates average ?
i thought this should be rather:
Code Java:
average = (goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4])/5;