Using Java to Find Mean and Standard Deviation
Submitted by GeePee on Friday, May 8, 2015 - 22:41.
Today, we are going to work with a program that accepts user input in the form of five numbers, and calculates their mean and standard deviation. With that in mind, let's get right into the code itself:
The amount of numbers doesn't necessarily have to be five, but for simplicity's sake, and to focus more on what this tutorial was intended for, allowing the user to control the number of variables will be covered in a subsequent tutorial.
As you can see in the main method above, the user is prompted for five numbers at a time. They can all be stored one after another with the following code:
The calculation for the mean is pretty simple. The five numbers were added, and then divided by five. However, the standard deviation was calculated using the following line of code:
- /*
- This program accepts input from a user in the form of five numbers, and calculates both their mean and standard deviation. Separate methods are used for calculating each of them.
- */
- import java.util.Scanner; //imports scanner class
- public class stats
- {
- {
- double num1 = input.nextDouble(); //these five lines store the numbers the user enters
- double num2 = input.nextDouble();
- double num3 = input.nextDouble();
- double num4 = input.nextDouble();
- double num5 = input.nextDouble();
- double avg = average(num1, num2, num3, num4, num5); //sends the input numbers to average method
- double dev = standdev(num1, num2, num3, num4, num5, avg); //sends input numbers and the average to standdev method
- System.out.println("\nThe standard deviation of the numbers entered is: " + dev + "\n"); //output to user for standard deviation
- }
- public static double average(double a, double b, double c, double d, double e) //average method for calculating mean
- {
- double value = ((a + b + c + d + e)/5); //calculated the mean of the five numbers
- return value; //returns value to main
- }
- public static double standdev(double a, double b, double c, double d, double e, double f) //standev method for calculating standard deviation
- {
- double s = Math.sqrt(((a-f)*(a-f) + (b-f)*(b-f) + (c-f)*(c-f) + (d-f)*(d-f) + (e-f)*(e-f))/5); //calculates the standard deviation
- return s; //returns standard deviation to the main
- }
- }
- double num1 = input.nextDouble();
- double num2 = input.nextDouble();
- double num3 = input.nextDouble();
- double num4 = input.nextDouble();
- double num5 = input.nextDouble();
double s = Math.sqrt(((a-f)*(a-f) + (b-f)*(b-f) + (c-f)*(c-f) + (d-f)*(d-f) + (e-f)*(e-f))/5);
This may look very messy, but it is just the standard deviation formula found here.
When a variable is passed to another method, it can be returned to the main by using return followed by the variable that is going to be returned. Have a look at both the average and standdev methods above for examples of this. The values are returned to the main, and they are printed to the screen for the user at the end.
Questions? Please let me know in the comments below.Add new comment
- 236 views