CS 139 Algorithm Development
Lab11B: Standard Deviation

Objectives

Students will be able to:

Background

Standard deviation is a measure of the spread of values around a mean. The higher the standard deviation, the more the values are scattered away from the mean. To calculate, one must first calculate the mean (average) and then use that mean to calculate the distance of each value from the mean. These distances are squared to account for numbers both less than and greater than the mean value.

From Wikipedia:

formula

In other words, the standard deviation of a discrete uniform random variable X can be calculated as follows:

1. For each value xi calculate the difference xi - x between xi and the average value x.

2. Calculate the squares of these differences.  Sum the squares of the distances.

3. Find the average of the squared differences. This quantity is the variance σ2.

4. Take the square root of the variance.

Key Terms

Command line argument - a value passed to a program called from the command line.

Mean - Average

Standard deviation - A measure of the spread of values around a mean.

Materials

You will write this program from scratch.

You may use the following file for testing: testData.

To use the file, run your program in the command line environment and override standard input. See demo at the beginning of class for this technique.


Part 1 General Instructions:

  1. Setup your program environment.
  2. Download the testData file.
  3. Create a new program file StdDev.java, and StdDevHelper.java.

Part 2 Creating an array from input values:

  1. You will implement your program in two files.  
  2. The main function (in StdDev.java) will read in a series of numbers, create an array containing those numbers and then call the mean and standard deviation functions from the other file, reporting on the results.
    1. If the main program is passed 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.Realize that the argument list is an array of Strings. (see the Integer.parseInt() method documentation for converting a string to an integer).
    2. Create an array of double that is built to the size of the command line argument or 25 as a default if no argument is provided.
    3. To read in the values, you will have a loop which will read the 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 in.
    4. Once the array is built, you will pass it to functions in StdDevHelper.java.
    5. You should output the values as shown below in Output.
    6. The only output from the program will be a single line using format "Mean: %.2f\n" containing the calculated mean value.This output line will be placed in the StdDev.java program.
    7. If you want to test your program at this point, create a "stub" for the method in Part 3.
    8. To test the program using the command line argument for the number of values to read in, go to the terminal window, navigate to your work folder, and enter: java StdDev 25 This command will run the StdDev program passing 25 in as the command line argument to the main method. If you run it with just java StdDev, then the default of 25 values should be used.

    Part 3 Creating a method to calculate the mean.

    1. In the StdDevHelper.java file you will create a function (static method). mean, will take in an array of double values and calculate and return the mean(average) of those numbers.
    2. Your method should insure that the array is not null and that it has more than 0 elements. If either of those conditions is true, return 0.
    3. Test your mean method from the StdDev.java program. See Part 2- step 8 for using the command line and passing in the appropriate number of values (use a small number of values so you can check your mean easily).

    Part 4 Calculating the Standard deviation:

    1. Add to your Helper file a function, stdDev, that computes the standard deviation of a passed array of double numbers. See the formula above in background.
    2. Your standardDeviation function should use the mean function created in Part 3 above.
    3. Again, you should insure that the array is not null and that it has more than 0 elements. If either of those two conditions is true, return -1.
    4. Add to the StdDev program a single line of output following the mean line.The format of the standard deviation line should use format string "Standard Deviation: %.2f\n".
    5. Test your program.

    Part 5 Redirecting input:

    1. By now you are probably tired of typing in all the values. Wouldn't it be nice to just have a file that you could use for testing? Set it up once and then use it.
    2. Without changing the program, you can!
    3. If you have not already done so, download the testData.txt file into the folder containing your program.
    4. Go to the terminal window and navigate to your work folder.
    5. At the command line, type the command java StdDev 25 <testData.txt. This command is interpreted as run StdDev using 25 for the command line argument to the program and redirect input (<) to use testData.txt instead of the normal System.in.

    Part 6 - Optional - Coding Bat Practice with Arrays

    1. Arrays-2, countEvens: http://codingbat.com/prob/p162010
    2. Arrays-2, matchUp: http://codingbat.com/prob/p136254

    Updated 11/07/12 (nlh)