Hey i need some help with my java programming, im using jgrasp to code, the prob
ID: 3865770 • Letter: H
Question
Hey i need some help with my java programming, im using jgrasp to code, the problems critera is below.
Write a method sDev that accepts an array of doubles as a parameter. The sDev method should calculate and return the standard deviation of the numbers passed in by way of the array. If side calculations are needed to solve this problem, write a helper method to do so.
The equation for standard deviation is as follows:
In other words sum up ((every point - average) squared), divide by count of points -1 and square root the entire quantity.
Minor points will be deducted if all logic is in the same method. However please don't go crazy with the method creation. Only create a new method when you identify a significant standalone task that needs to be done.
Turn in one properly commented and styled java file for this part.
Explanation / Answer
import java.io.*;
import java.util.*;
public class chegg_sdev {
public static void sDev(double[] mylist,int n)
{
double sum=0,mean=0,sd=0;
sum=sum_double(mylist,n);
mean=sum/n;
for (int i=0; i<n; i++) //calculate standard deviation
{
sd=sd+Math.pow((mylist[i]-mean),2);
}
System.out.println("Standard Deviation:" + Math.sqrt(sd/n));
}
public static double sum_double(double[] mylist,int n)
{
double sum=0;
for (int i=0; i<n; i++) //Take input in the array
{
sum=sum+mylist[i];
}
return sum;
}
public static void main(String[] args) throws Exception {
Integer n;
System.out.println("Enter number of inputs");
Scanner sc = new Scanner(System.in);
n=sc.nextInt();
double[] mylist = new double[n];
for (int i=0; i<n; i++) //Take input in the array
{
System.out.print("Enter a number : ");
mylist[i]=sc.nextDouble();
}
sDev(mylist,n);
}
}
Output:
Enter number of inputs
3
Enter a number : 1
Enter a number : 2
Enter a number : 3
Standard Deviation:0.816496580927726