For this assignment, you will be creating a method which will be used later in t
ID: 3704052 • Letter: F
Question
For this assignment, you will be creating a method which will be used later in the project.
Write a method named average:
Write a method named median which calculate, and return, the average of an array of integers. The method should receive as parameters, an array of integers and the filled size of the array.
Your method should assume that the array will contain some positive integers, filled in ascending order. This is a partially-filled array, where the number of items in the array may be less than the declared size of the array. The size parameter tells the method how many items are actually stored in the array.
The average should be calculated as a double value.
Main program:
There is no main program required for this part. If you wish to test this method yourself outside of Mimir, then you would need to write a small main program which could be used to test your average method.
Start Code:
public class CalculateAverage {
/**
* Calculates the average of the integers in array 'list'
* @param list The array containing the integers for which to calculate the average
* @param size The number of integers in array 'list'
* @return The average of the integers in the array
*/
public static double average(int [] list, int size) {
return 0;
}
}
Explanation / Answer
CalculateAverage.java
public class CalculateAverage {
/**
* Calculates the average of the integers in array 'list'
* @param list The array containing the integers for which to calculate the average
* @param size The number of integers in array 'list'
* @return The average of the integers in the array
*/
public static double average(int [] list, int size) {
int total = 0;
for(int i=0;i<size;i++) {
total+=list[i];
}
return total/(double)size;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.