Lab16: Computing basic statistics


Standard deviation is a measure of the spread of a set of values around their mean. The higher the standard deviation, the more the values are scattered away from the mean. See the Wikipedia article for more details and examples.



  • Read input data into an array.
  • Compute statistics using an array.
  • Process command-line arguments.

Part 1: Understand the algorithm

Given a set of N values (x1 through xn) with mean x, the standard deviation σ is:

Standard Deviation

In other words, you need to write two loops: one that computes the mean x, and another that computes the average of the squared differences (i.e., the variance). At the end, take the square root of the variance to determine the standard deviation.

  1. Compute the mean and standard deviation for the following numbers by hand: {82, 88, 97, 80, 79, 92}
  2. Make sure you get 86.3 for the mean and 6.6 for the standard deviation. Think about the algorithm you used.

Part 2: Creating an array from input values

  1. Create two Java classes for today's lab: with a main method, and without a main method. The main method will read a series of numbers from, create an array containing those numbers, call the mean and standard deviation functions in the Stats class, and then report the results.
  2. If the main method is given a command-line argument, that argument will specify the number of values to be read. If no command-line argument is passed, the program should read 25 values. Note that the argument args is an array of Strings. See the Integer.parseInt() documentation for converting a String to an int.
  3. Create an array of double values, using the size from the command-line argument (or 25 as a default, if no argument is provided).
  4. Use a loop to read the input values from the keyboard (as doubles) and store them into the array. No prompt should be output before reading the values. The loop should terminate when the appropriate number of values has been read.
  5. Once the array is built, you will pass it to methods in For now, simply print the contents of the array to make sure your program is working correctly. You may find the Arrays.toString method useful.
  6. To test the program using the command-line argument for the number of values to read in, open a terminal window, navigate to your work folder, and enter:
    java Main 10
    This command will run the Main program passing {"10"} as the command-line arguments to the main method. If you run it with just java Main then the default of 25 values should be used.

Do not move on until you know the input is working, both with and without a command-line argument!

Part 3: Writing a method to calculate the mean

  1. In the file, create a static method called mean that will take an array of double values as its parameter, and then calculate and return the mean of those numbers.
  2. The method should ensure that the array is not null and that it has more than 0 elements. If either of these conditions exists, the method should return Double.NaN (not a number).
  3. Test your mean method from the program. The main method should output the return value as a single line, using the format "Mean: %.2f\n".

Part 4: Calculating the standard deviation

  1. Add to a method called stdDev that computes the standard deviation of a given array of double numbers.
  2. Your stdDev method should call the mean method created in Part 3 above.
  3. The method should ensure that the array is not null and that it has more than 0 elements. If either of those conditions exists, return Double.NaN (not a number).
  4. Add to the Main program a single line of output following the mean output line. The format of the standard deviation line should be "StdDev: %.2f\n".

Part 5: More command-line tips and tricks

By now you are probably tired of typing in all the values during testing. Wouldn't it be nice to have a file that you could use for testing, so that you could set it up once and then use it repeatedly?

  1. Download the file testData.txt into the folder containing your program. At the command-line, type the command:
    java Main 20 < testData.txt
    In other words, run Main using 25 for the command-line argument, and redirect input to use testData.txt instead of the normal
  2. The redirect operator (<) is a feature of the operating system shell, which is the program that is reading and processing the commands that you enter in the terminal window. It's also possible to redirect the output of your program to a file, using the > symbol, as in:
    java Main 20 < testData.txt > output.txt
  3. You can do a lot more with the terminal than what we've seen in this course. For example, it's possible to write simple for loops that repeat the same command with different arguments:
    for n in 5 10 12; 
    do java Main $n < testData.txt; 

  4. In other words, first run Main with 5, then with 10, then with 12. Try running this command in your terminal, and experiment with different arguments (separated by spaces).

Submit your and files via Canvas by the end of the day.

Back to Top