calculate the mean and standard deviation of a set of n real numbers using java

import java.util.*;

public class DataSetTester  
{  
   public static void main(String[] args)  
   {  
       DataSet a = new DataSet();  
       Scanner input=new Scanner(System.in);
       System.out.println("enter the total number of items");
       int m =input.nextInt();
       for(int i =1; i <=m; i++)
       {
           System.out.println("enter the "+i+"  number");
           a.add(input.nextInt());  
       }
   
    
      System.out.println("count: " + a.getCount());  

      System.out.println("Mean: " + a.getMean());  

      System.out.println("standard deviation: " + a.getStandardDeviation());  
      
   }  





/////////////////////////////






import java.util.ArrayList;  
import java.util.List;  
 

public class DataSet {  
 
    private List<Double> inputList = new ArrayList();  
    double x = 0;  

    public DataSet() {  
    }  
  
    public void add(double x) {  
 
        inputList.add(x);  
      
    }  
 

    public double getMean() {  
 
        double sum = getSum();  
        double count = getCount();  
        double mean = sum / count;  
 
        return mean;  
 
    }  
 
    public double getSum() {  
        double sum = 0;  
 
        for (double d : inputList) {  
            sum += d;  
        }  
        return sum;  
    }  
 
    public double getStandardDeviation() {  
 

        double sum = getSum();  
  
        double mean = getMean();  
        double calc1 = 0;  
        double calc2 = 0;  
        double count = getCount();  
        double stdDeviation = 0;  
 
        System.out.println("Sum = " + sum);  
 
        for (int i = 0; i < count; i++) {  
            calc1 = inputList.get(i) - mean;  
            calc1 = Math.pow(calc1, 2);  
            calc2 = calc2 + calc1;  
        }  

            calc2 = calc2 / (count-1);  
            stdDeviation = Math.sqrt(calc2);  
        return stdDeviation;  
    }  
   
    public int getCount() {  
        return inputList.size();  
 
    }  
}









output:




enter the total number of items
4
enter the 1  number
1
enter the 2  number
2
enter the 3  number
3
enter the 4  number
4
count: 4
Mean: 2.5
Sum = 10.0
standard deviation: 1.2909944487358056

1 comment :

  1. Fantastic beat ! I would like to apprentice while you amend your site, how can
    i subscribe for a blog site? The account helped me a acceptable
    deal. I had been a little bit acquainted of this your broadcast offered bright clear idea
    My website ; www.serenityeveryday.com

    ReplyDelete

Copyright © Rough Record. Designed by OddThemes