/**
 * CS139 - Programming Fundamentals
 * Department of Computer Science
 * James Madison University
 * @version Spring 2016
 */

Background

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.

Objectives

Part 1: Understand the algorithm

Given a set of \(N\) values (\(x_1\) through \(x_n\)) with mean \(\bar{x}\), the standard deviation \(\sigma\) is:

\[ \sigma = \sqrt{\frac{1}{N}\sum_{i=1}^N(x_i - \bar{x})^2}\]

Finding the standard deviation involves two loops: one that computes the mean \(\bar{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: StatDriver.java with a main method, and Stats.java without a main method. The main method will read a series of numbers from System.in, 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 15 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 15 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 Stats.java. 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 StatDriver 10
    This command will run the driver program passing {"10"} as the command-line arguments to the main method. If you run it with just java StatDriver then the default of 15 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 Stats.java 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 StatDriver.java 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 a method calledstdDev to Stats.java. This method should compute 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 driver 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 StatDriver 20 < testData.txt

    In other words, run the driver using 15 for the command-line argument, and redirect input to use testData.txt instead of the normal System.in.

  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 StatDriver 20 < testData.txt > output.txt

Create a zip file containing StatDriver.java and Stats.java files and submit it through WebCat. Web-CAT will not run Checkstyle tests on your submission.

Part 6: If You Have Extra Time

  1. Add a median method to your Stats.java class. (The Arrays.sort method will probably be helpful.)