# Thread: Average calculation in a separated class

1. Member Join Date
Sep 2016
Posts
3
Rep Power
0

## Average calculation in a separated class

Hi,
I want to calculate the average of 4 inputs, I've created "Average" class but there is something wrong with that :

Java Code:
```package averageinclass;

import java.util.*;

public class AverageInClass {

public static void main(String[] args) {

Scanner input= new Scanner(System.in);

Average ave= new Average();

double []x = new double;

for (int i = 0; i < 4; i++) {

x[i]=input.nextDouble();

}

System.out.print("The average of the given numbers is" +Math.ceil(+ave.Average() ) );

} }```

Java Code:
```public class Average {

public double Average(double []x) {

double sum=0;

for (int i = 0; i < x.length; i++) {

sum=sum+x[i];

}
double average = sum/4;
return  average;
}

}```
Last edited by Pedram.; 09-13-2016 at 01:32 PM.  Reply With Quote

2. ## Re: Average calculation in a separated class

there is something wrong with that :
Please explain. If there are errors, copy the full text of the error message and paste it here.  Reply With Quote

3. Member Join Date
Sep 2016
Posts
3
Rep Power
0

## Re: Average calculation in a separated class  Reply With Quote

4. ## Re: Average calculation in a separated class

The method called on line 23 requires an argument of an array of doubles.

Note: The computation of the average on line 18 should use the .length of the array like on line 13, not a hardcoded 4. That would allow the method to compute the average of any sized array.
Last edited by Norm; 09-13-2016 at 02:28 PM.  Reply With Quote

5. Member Join Date
Sep 2016
Posts
3
Rep Power
0

## Re: Average calculation in a separated class

So the problem was the lack of the x argument in the line 23 :
Java Code:
` System.out.print("The average of the given numbers is" +Math.ceil(+ave.Average(x) ) )`
;

I did some modifications and the altered code is :
Java Code:
```package averageinclass;

import java.util.*;

public class AverageinClass {

public static void main(String[] args) {

Scanner input= new Scanner(System.in);

Average ave= new Average();

System.out.println("Enter 0 as the last number");

int n=1000;
int j=0;
double []x = new double[n];

for (int i = 0; i < x.length; i++) {

x[i]=input.nextDouble();
j++;
if (x[i]==0) {
break;
}
}

System.out.println("The average of the given numbers is " +ave.Average(x,j)  );

} }```

Java Code:
```package averageinclass;

public class Average {

public double Average(double []x, int j) {

double sum=0;

for (int i = 0; i <j; i++) {

sum=sum+x[i];

}
double average = sum/j;
return  average;
}

}```  Reply With Quote

6. ## Re: Average calculation in a separated class

What is the variable j for? The name of a variable should describe what the variable contains. For example: numberUsed would be a good name for a variable that contained the number of elements in an array that were used to hold data.
Last edited by Norm; 09-14-2016 at 04:22 PM.  Reply With Quote

average, class, coding, java 