Using Java to Find Mean and Standard Deviation

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:
  1. /*
  2. 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.
  3. */
  4.  
  5. import java.util.Scanner; //imports scanner class
  6.  
  7. public class stats
  8. {
  9.  
  10.     public static void main(String[] args)
  11.     {
  12.    
  13.         Scanner input = new Scanner(System.in); //creation of scanner object called input
  14.        
  15.         System.out.print("\nEnter five numbers, hitting enter after each one: \n"); //prompts user
  16.        
  17.         double num1 = input.nextDouble(); //these five lines store the numbers the user enters
  18.        
  19.         double num2 = input.nextDouble();
  20.        
  21.         double num3 = input.nextDouble();
  22.        
  23.         double num4 = input.nextDouble();
  24.        
  25.         double num5 = input.nextDouble();
  26.        
  27.         double avg = average(num1, num2, num3, num4, num5); //sends the input numbers to average method
  28.        
  29.         double dev = standdev(num1, num2, num3, num4, num5, avg); //sends input numbers and the average to standdev method
  30.        
  31.         System.out.println("\nThe average of the numbers entered is: " + avg); //output to user for mean
  32.        
  33.         System.out.println("\nThe standard deviation of the numbers entered is: " + dev + "\n"); //output to user for standard deviation
  34.        
  35.     }
  36.    
  37.     public static double average(double a, double b, double c, double d, double e) //average method for calculating mean
  38.  
  39.     {
  40.         double value = ((a + b + c + d + e)/5); //calculated the mean of the five numbers
  41.         return value; //returns value to main
  42.     }
  43.    
  44.     public static double standdev(double a, double b, double c, double d, double e, double f) //standev method for calculating standard deviation
  45.  
  46.     {
  47.         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
  48.         return s; //returns standard deviation to the main
  49.     }
  50. }
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:
  1. System.out.print("\nEnter five numbers, hitting enter after each one: \n");
  2.        
  3.         double num1 = input.nextDouble();
  4.        
  5.         double num2 = input.nextDouble();
  6.        
  7.         double num3 = input.nextDouble();
  8.        
  9.         double num4 = input.nextDouble();
  10.        
  11.         double num5 = input.nextDouble();
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: 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