Answer:
The method in Java is as follows:
public static int computeOddSum(int [] myarray){
int oddsum = 0;
for(int i=0; i<myarray.length; i++ ) {
if(myarray[i]%2==1) {
oddsum+=myarray[i];
}
}
return oddsum;
}
Explanation:
This defines the static method
public static int computeOddSum(int [] myarray){
This initializes oddsum to 0
int oddsum = 0;
This iterates through the array
for(int i=0; i<myarray.length; i++ ) {
This checks for odd number
if(myarray[i]%2==1) {
The odd numbers are added, here
oddsum+=myarray[i];
}
}
This returns the calculated sum of odd numbers
return oddsum;
}
To call the method from main, use:
<em>int [] myarray = {3, 10, 11, 2, 6, 9, 5};</em>
<em> System.out.println("Odd sum = " + computeOddSum(myarray));</em>