CS174: OOP - Drills - Computing the mean of arrays

Developed by Professor Tralie and Professor Mongan.


Exercise Goals

The goals of this exercise are:
  1. To do proper array indexing
  2. To use loops in concert with arrays
  3. To declare accumulator variables outside of loops that are used in loops, but whose state persists beyond the loop
  4. To use proper types
  5. To handle boundary cases
Fill in a method to compute the mean of an array of ints. Note that even though the inputs are integers, their mean may be a decimal number! For example, the mean of {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
Clicking Run below will check your work and, if it passes, will submit your work automatically. You must be connected to the VPN for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail!

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);

Output