CS 139 Algorithm Development
Lab12A: Standard Deviation

Objectives

Students will be able to:

Background

Standard deviation is a measure of the spread of a set of values around a mean. The higher the standard deviation, the more the values are scattered away from the mean.

To calculate standard deviation, first calculate the mean (average) of the set, 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. Squaring makes all distances positive. The square root operator is applied after the mean is taken, to rescale the result. http://www.mathsisfun.com/data/standard-deviation-formulas.html for a further explanation.

This site has a calculator that you can use to check your work: http://www.miniwebtool.com/population-standard-deviation-calculator/

From Wikipedia:

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

formula

The population 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 square of each difference, and sum the squares.

3. Find the average of the squared differences. This quantity is the variance2).

4. Take the square root of the variance to determine the standard deviation.

Key Terms

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. Create a new folder for your files.
  2. Download the testData file into the folder.
  3. Create two new program file StdDev.java, and StdDevHelper.java.

Part 2 Creating an array from input values:

You will implement your program in two files, StdDev.java and StdDevHelper.java. The main function (in StdDev.java) will read a series of numbers, create an array containing those numbers, call the mean and standard deviation functions from StdDevHelper,  and then report 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. Note that the argument list is an array of Strings. (see the Integer.parseInt() method documentation for converting a string to an integer). Use the following code to do this:
     
              if (args.length > 0)
                 size = Integer.parseInt(args[0]);
              else
                 size = 25;
            
  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 the input values, use a loop which will read the values from the keyboard (as doubles) and store them in the array. No prompt should be output before reading the values. The loop should terminate when the appropriate number of values has been read.

  4. Once the array is built, pass it to functions in StdDevHelper.java.

  5. Output the values as a single line, "Mean: %.2f\n", containing the calculated mean value. This output should be performed by StdDev.java.

  6. In order to test your program at this point, create a "stub" for the method described in Part 3.  (A stub is an empty method that returns zero or null.)

  7. 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 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, create a function (a static method), 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 insure that the array is not null and that it has more than 0 elements. If either of these conditions exists, the method should 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 StdDevHelper a function, stdDev, that computes the standard deviation of a passed array of double numbers. See the formula above in the Background section.

  2. Your standardDeviation function should use the mean function 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 -1.

  4. Add to the StdDev program a single line of output following the mean output line. The format of the standard deviation line should be "Standard Deviation: %.2f\n".

  5. Test your program.

Part 5 Redirecting input:

By now you are probably tired of typing in all the values during testing. Wouldn't it be nice to just have a file that you could use for testing, so that you could set it up once and then use it repeatedly? Without changing the program, you can!
  1. If you have not already done so, download the testData.txt file into the folder containing your program.

  2. Go to the terminal window and navigate to your work folder.

  3. 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 and redirect input to use testData.txt instead of the normal System.in.

    The redirect operator (<) is a tool of the command 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 StdDev 25 <testData.txt  >save_output.txt

Part 6 Turning in your work:

Submit this program to WebCAT for accuracy checking.


Part 7 - 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/03/13 (nlh)