CS174: OOP - Drills - Computing the mean of arrays
Developed by Professor Tralie and Professor Mongan.
Exercise Goals
The goals of this exercise are:- To do proper array indexing
- To use loops in concert with arrays
- To declare accumulator variables outside of loops that are used in loops, but whose state persists beyond the loop
- To use proper types
- To handle boundary cases
{0, 5, 2, 4}
is 2.75
. Finally, if an empty array is passed to your method, you should return 0.0. Recall that this is referred to as a boundary case or edge case in testing.
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)
Netid |
ArrayUtils.java
public class ArrayUtils {
public static double getMean(int[] arr) {
/** TODO: Fill in your code here to compute the mean **/
return 0.0;
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
int[] arr0 = {0, 5, 10, 0, 3, 4, 0, 1};
double mean0 = (double)ArrayUtils.getMean(arr0);
int[] arr1 = {0, 0, 1, 1, 1, 1, 3};
double mean1 = (double)ArrayUtils.getMean(arr1);
int[] arr2 = {};
double mean2 = (double)ArrayUtils.getMean(arr2);
System.out.print(mean0 + "." + mean1 + "." + mean2);
}
}
Tester.main(null);