Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mean and standard deviation. Write a program that reads a set of floating-point

ID: 3864960 • Letter: M

Question

Mean and standard deviation. Write a program that reads a set of floating-point data values. Choose an appropriate mechanism for prompting for the end of the data set. When all values have been read, print out the count of the values, the average, and the standard deviation. The average of a data set {x_1 ..., x_n} is x = sigma x_i/n, where sigma x_i = x_1 + ... + x_n is the sum of the input values. The standard deviation is s = Squareroot sigma(x_i - x)^2/n - 1 However, this formula is not suitable for the task. By the time the program has computed x, the individual x_i are long gone. Until you know how to save these values, use the numerically less stable formula s = Squareroot sigma x_i^2 - 1/n (sigma x_i)^2/n - 1 You can compute this quantity by keeping track of the count, the sum, and the sum of squares as you process the input values. Your program should use a class DataSet. That class should have a method public void add(double value) and methods getAverage and getStandardDeviation.

Explanation / Answer

DataSet.java

import java.util.ArrayList;

public class DataSet {
   //Declaring ArrayList reference variable
   ArrayList<Double> arl;

   //Zero argumented constructor
   public DataSet() {
       super();
       this.arl = new ArrayList<Double>();
   }
  
   //This method will add the values to the ArrayList
   public void add(double value)
   {
       arl.add(value);
   }
  
   //This method will find the average of the values present in the ArrayList
   public double getAverage()
   {
       double sum=0.0,average;
       for(Double d:arl)
       {
           sum+=d;
       }
       average=sum/arl.size();
       return average;
   }
  
   /* this method will find the Standard Deviation
   * of the Number present in the ArrayList
   */
   public double getStandardDeviation()
   {
double variance,std_dev,sum=0.0;
double mean=getAverage();
int count=arl.size();
for(Double d:arl)
{
sum+=Math.pow(d-mean,2);
}
//calculating the standard deviation of nos[] array
variance=(double)sum/(count-1);
  
return Math.sqrt(variance);

   }
  
  

}

________________

Driver.java

import java.text.DecimalFormat;
import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {
  
       //Declaring a constant
       final int SENTINAL=-1;
      
       //Declaring a variable
       double num;
                  
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Creating the object for the DataSet class
       DataSet ds=new DataSet();
int i=0;
  
/* getting the value entered by the user
* and populating the values into an ArrayList
* by using the add() method on the DataSet class
*/
       while(true)
       {
           //Getting the value entered by the user
           System.out.print("Enter the number (-1 to Quit)"+(i+1)+":");
           num=sc.nextDouble();
           if(num!=SENTINAL)
           {
               //calling the method on the DataSet Object
               ds.add(num);
               i++;
           }
           else
               break;
       }
      
       //Displaying the No of values User entered
       System.out.println("No of values you entered :"+i);
      
       //Displaying the Average of te numbers
       System.out.printf("Average :%.2f",ds.getAverage());
      
       //Displaying the Standard Deviation
       System.out.printf(" Standard Deviation :%.2f",ds.getStandardDeviation());
      
      

   }

}

__________________

Output:

Enter the number(-1 to Quit) 1:12.3
Enter the number(-1 to Quit) 2:23.4
Enter the number(-1 to Quit) 3:34.5
Enter the number(-1 to Quit) 4:45.6
Enter the number(-1 to Quit) 5:56.7
Enter the number(-1 to Quit) 6:67.8
Enter the number(-1 to Quit) 7:78.9
Enter the number(-1 to Quit) 8:98.7
Enter the number(-1 to Quit) 9:76.5
Enter the number(-1 to Quit) 10:54.3
Enter the number 11:-1
No of values you entered :10
Averge :54.87
Standard Deviation :26.75

____________Thank You