JavaPerspective.com >
Beginner Tutorials >
5. Java in Practice >
5.8. Java code exercises >
5.8.4. Code exercise 4
5.8.4. Code exercise 4
Last updated: 27 January 2013.
Write a method named getAverage(float[] array) that returns the average of float values in the given array argument. If the array argument is null, the method returns 0.
public final class Practice {
public float getAverage(float[] array){
// the code goes here
}
}
public float getAverage(float[] array){
// the code goes here
}
}
public final class Practice {
public float getAverage(float[] array){
if(array == null)
return 0;
float sum = 0;
for(float value : array)
sum = sum + value;
return (sum / array.length);
}
}
public float getAverage(float[] array){
if(array == null)
return 0;
float sum = 0;
for(float value : array)
sum = sum + value;
return (sum / array.length);
}
}
You are here :
JavaPerspective.com >
Beginner Tutorials >
5. Java in Practice >
5.8. Java code exercises >
5.8.4. Code exercise 4
Next tutorial : JavaPerspective.com > Beginner Tutorials > 5. Java in Practice > 5.8. Java code exercises > 5.8.5. Code exercise 5
Next tutorial : JavaPerspective.com > Beginner Tutorials > 5. Java in Practice > 5.8. Java code exercises > 5.8.5. Code exercise 5