double[] originalArray = {92.0, 69.0, 35.0, 27.0, 9.0, 83.0, 89.0};
double averageOfUpperHalfValues = findAverageOfUpperHalfValues(originalArray);
System.out.println(averageOfUpperHalfValues);
private double findAverageOfUpperHalfValues(double[] array) {
double median = findMedian(array);
double[] upperHalf = filterArrayByMinimumValue(inputs, minimum);
double averageOfUpperHalf = findAverage(upperHalf);
return averageOfUpperHalf;
}
private double findMedian(double[] array) {
// Write some code to find the median value from an array.
}
private double[] filterArrayByMinimumValue(double[] array, double minimum) {
// Write some code to return a smaller array of just the bigger values.
}
private double findAverage(double[] array) {
// Write some code to find the average value from an array.
}
There's a basic structure for you. The first method really just lists all the stuff that's going to happen. The last three methods contain the actual workings, split into separate activities so they are easy to read, test and reuse. Made sure you do test them!!!
Assuming you understand what the median is, findMedian(double[] array)
should be fairly straigtforward. Hint: within this method, call another method to sort the array.
filterArrayByMinimumValue(double[] array)
has at least two possible approaches. 1) trawl through the input array to count how many items will be in the filtered array, make an array of that size, then go back through the original array to find the values you want to keep and put them in the new array; return the new array. 2) make a collection type which doesn't have to have its size known in advance, go through the original array just once and copy all the values you want to keep over to your new collection, then convert the collection into an array of doubles and return it.
findAverage(double[] array)
has an immediate point of concern. There are three different types of 'average': mean, median and mode. Clarify which of these you want (probably the mean, but know the differences and be certain). Change the method name accordingly. Write the code to do the job. If I were you, I think I would write this method first because it looks easiest. No shame in starting off with a psychological boost :)
If you have trouble with figuring out the code for any one of these three methods, come back to us with your best attempt and tell us what's puzzling you.