Use 3 methods(Addition, Average, numberEvenOdd) in an array

Viewed 29

I have an array named tab with several numbers

int[] tab = {1,3,4,2};

I have to create 3 methods:

1) the first addition() method

2) the second average() method

3) the third numberEvenOdd() method

My goal : I have to calculate the average of the odd numbers in the array by using my 3 methods.

Here is my addition() method

public static int addition(int[] tab){
            int sum = 0;
            for(int i=0;i<tab.length;i++){
                sum += tab[i];
            }
            return sum;
        }

Then, my average() method

public static double averages(int[] tab){
            return (double) addition(tab) / tab.length;
}

And my numberOddEven() method

public static int numberOddAdd(int[] tab, boolean value){
    int parity = 0;
    if(! value){
        parity = 1;
     }

    int n = 0;
    for(int i=0; i<tab.length;i++){
         if(tab[i] % 2 == parity){
             n += tab[i];
         }
     }
    return n;
    }

In my print, I have a problem in the syntax ?

I have tried this

System.out.println(averages(numberOddEven(tableau1, false)); 

My error message is -> incompatible types: int cannot be converted to int ?

0 Answers
Related