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

» You are to implement the class STDcalculator. Start with the code from the dem

ID: 3705973 • Letter: #

Question

» You are to implement the class STDcalculator. Start with the code from the demo exercise and add the method to calculate standard deviation. This method should be called calculateSTD and it should take one argument, an array of doubles, and it should return a double which is the standard deviation. » The equation for this calculation is as follows: ?(x_x) where n is the number of values, x is the ith value and x bar is the average of all the values Your output should match the following: The average of the numbers in file: numbers.csv is 262.634 and standard deviation: 136.666 Program terminating The average of the numbers in file: numbers2.cav is 258.731 and standard deviation: 148.680 Program terminating.

Explanation / Answer

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

public class STDCalculator {

public static void main(String[] args) {

String csvFile = "number.csv";

BufferedReader br = null;

double avg;

String line = "";

String cvsSplitBy = ",";

int sum=0;

String[] country = null;

try {

br = new BufferedReader(new FileReader(csvFile));

while ((line = br.readLine()) != null) {

// use comma as separator

country = line.split(cvsSplitBy);

}

int []array=new int[country.length];

for(int i=0;i<country.length ;i++){

array[i]=Integer.parseInt(country[i]);

//System.out.println(country[i]);

sum=sum+array[i];

}

avg=sum/(double)(country.length);

double sd = 0;

for (int i = 0; i < array.length; i++)

{

sd += Math.pow(array[i] - avg,2) / array.length;

}

double standardDeviation = Math.sqrt(sd);

System.out.println("The average of number in file: "+csvFile);

System.out.println("is "+avg+" and standard deviation: "+standardDeviation);

System.out.println();

System.out.println("Program Terminating.");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

input:

value in number.csv= 11,22,1,8,5

Output:

The average of number in file: number.csv
is 9.4 and standard deviation: 7.116178749862878

Program Terminating.