Question

In: Computer Science

I want to calculate the max, min, mean, standard deviation and the percentage of data within...

I want to calculate the max, min, mean, standard deviation and the percentage of data within one standard deviation from a file already existing in my directory using JAVA. As you can see it below, I have found max, min, mean and stand deviation. How can I get the percentages of data within one standard deviation in the following code?

import java.io.*;
import java.util.Scanner;

public class DataFile {

public static void main(String[] args) {
    // declare variables
    double number, maximum, minimum, sum, mean, standardDeviation;
    int count;

    Scanner file = null;

/* -------------------------------------------------------------------------- */
    try {
        file = new Scanner(new File("RawData.txt"));
    }

    catch(FileNotFoundException e) {
        System.out.print("Error; The program was terminated!");
        System.exit(1);
    }

/* -------------------------------------------------------------------------- */
    // initialize variables
    maximum = file.nextDouble();
    minimum = maximum;
    sum = 0;
    count = 1;

    while(file.hasNextDouble()) {
        number = file.nextDouble();

        if(number > maximum)
            maximum = number;

        else if(number < minimum)
            minimum = number;

        sum += number;
        count += 1;

    } // end while loop

    file.close();

/* -------------------------------------------------------------------------- */
    // mean calculation
    mean = sum / count;

    // standard deviation calculation
    
double stdDevSum = 0;
double stdDevMean = 0;
double stdDev = 0;

double sumOfSquares = squareSum - ((Math.pow(computationalSum, 2)/(count-1)));
double sSquared = sumOfSquares/(count-1);
double otherStdDev = Math.sqrt(sSquared);

// display statistics
System.out.println("Maximum ------------> " + maximum                   );
System.out.println("Minimum ------------> " + minimum                   );
System.out.println("Sum ----------------> " + sum                       );
System.out.println("Count --------------> " + count                     );
System.out.println("Mean ---------------> " + mean                      );
System.out.println("StdDev -------------> " + otherStdDev);

    

} // end method main

} // end class DataFile

Solutions

Expert Solution

I can tell you how to calculate percentage of data within one standard deviation

For example, you have find the value of the mean as 15.24 and value of standard deviation is 3.25. That means that the range of one standard deviation is from 15.24 + 3.25 to 15.24 -3.25 or 18.49 to 11.99

By rounding the values, I can say that values which falls between the range 12 to 18 to the total number of values in the file

percentage of data within one standard deviation = ( values between 12 to 18 / total number values in the file )

I have modified your code to make it work

import java.io.*;
import java.util.Scanner;

public class DataFile {
    public static void main(String[] args) {
        // declare variables
        double number, maximum, minimum, sum, mean, standardDeviation, percentageOfStandardDeviation;
        int count;
        Scanner file = null;
        try {
            file = new Scanner(new File("RawData.txt"));
        } catch(FileNotFoundException e) {
            System.out.print("Error; The program was terminated!");
            System.exit(1);
        }
        maximum = file.nextDouble();
        minimum = maximum;
        sum = 0;
        count = 1;
        while(file.hasNextDouble()) {
            number = file.nextDouble();
            if(number > maximum)
                maximum = number;
            else if(number < minimum)
                minimum = number;
            sum += number;
            count += 1;
        } 
        file.close();

        // mean calculation
        mean = sum / count;
        // Standard Deviation
        standardDeviation = 0;
        try {
            file = new Scanner(new File("RawData.txt"));
        } catch(FileNotFoundException e) {
            System.out.print("Error; The program was terminated!");
            System.exit(1);
        }
        while(file.hasNextDouble()) {
            number = file.nextDouble();
            standardDeviation += Math.pow(number-mean, 2);
        }
        file.close();
        standardDeviation = Math.sqrt(standardDeviation/count);
        
        //calculating percentage of data within one standard deviation 
        try {
            file = new Scanner(new File("RawData.txt"));
        } catch(FileNotFoundException e) {
            System.out.print("Error; The program was terminated!");
            System.exit(1);
        }
        double standardDeviationMin = Math.round(mean - standardDeviation);
        double standardDeviationMax = Math.round(mean + standardDeviation);
        int anotherCount = 0;
        while(file.hasNextDouble()) {
            number = file.nextDouble();
            if(number >= standardDeviationMin && number <= standardDeviationMax) {
                anotherCount++;
            }
        }
        file.close();
        System.out.println(anotherCount);
        percentageOfStandardDeviation = ((double)(anotherCount*100/count));
        // display statistics
        System.out.println("Maximum --------------> " + maximum);
        System.out.println("Minimum --------------> " + minimum);
        System.out.println("Sum ------------------> " + sum);
        System.out.println("Count ----------------> " + count);
        System.out.println("Mean -----------------> " + mean);
        System.out.println("StdDev ---------------> " + standardDeviation);
        System.out.println("percentageOfStdDev----> " + percentageOfStandardDeviation);   
    } 
}

The output of the above code is:

I hope this helps you out. Thank you


Related Solutions

Calculate the standard deviation of the following test data by hand. Describe the mean and standard...
Calculate the standard deviation of the following test data by hand. Describe the mean and standard deviation in words after calculating it. Record the steps. Test Scores: 40, 25,50, 30, 60, 80, 20, 70
Given the data below, calculate the mean, calculate the standard deviation and explain the significance of...
Given the data below, calculate the mean, calculate the standard deviation and explain the significance of each in the context of the data. Grade on Final Exam Frequency 50 1 60 2 70 3 80 5 90 6 100 3
1. A population standard deviation is 5.  We want to estimate the population mean within 2, with...
1. A population standard deviation is 5.  We want to estimate the population mean within 2, with a 90% level of confidence. How large a sample size is required? 2. The estimate of the population proportion should be within plus or minus 0.05 with a 99% confidence level. The best estimate for the population is 0.42. How large a sample size is required?
a) Calculate the mean, variance, and standard deviation for each of the binomial distributions below. i)...
a) Calculate the mean, variance, and standard deviation for each of the binomial distributions below. i) ?~?(1000,0.05) ii) ?~?(800,0.25) b) Team Victory’s winning rate is 20% whenever it plays a match. If the team Victory played 10 matches, what is the probability: i) It will win exactly one match. ii) It will win at most two matches. c) On average, Ali scores a goal per match. What is the probability that Ali will score: i) No goals in the next...
Given the data set below, calculate the range, mean, variance, and standard deviation
Given the data set below, calculate the range, mean, variance, and standard deviation. 19,  33,  8,  29,  18,  5, 10, 14,  25 Range = Mean = Variance = Standard deviation =
Calculate the mean and standard deviation and interpret your findings for the following set of data...
Calculate the mean and standard deviation and interpret your findings for the following set of data showing the diastolic blood pressure measurements for a sample of 9 individuals: 61, 63, 64, 69, 71, 77, 80, 81, and 95. On average, the average distance of an individual data point is approximately 10.93 diastolic pressure points from the mean diastolic pressure of 73.44. On average, the average distance of an individual data point is approximately 119.53 diastolic pressure points from the mean...
A population is normally distributed with mean ? and standard deviation ?. Find the percentage of...
A population is normally distributed with mean ? and standard deviation ?. Find the percentage of values which are between ?−2? and ?+2?.
I have Standard Deviation and Mean of 2 sets of data. Based on the data, how...
I have Standard Deviation and Mean of 2 sets of data. Based on the data, how can we infer at the 5% significance level that the score of individuals in the 4th year is better than the individuals in 1st year? average 71.29 76.98 S.D. 8.58 8.119 Year 1 Year 4 The sample size is 430
This data is from a sample. Calculate the mean, standard deviation, and variance. x 38.3 35.2...
This data is from a sample. Calculate the mean, standard deviation, and variance. x 38.3 35.2 26.7 49.9 44.6 14.2 16.4 34 14.4 Please show the following answers to 2 decimal places. Sample Mean = Sample Standard Deviation = Sample Variance = Ooops - now you discover that the data was actually from a population! So now you must give the population standard deviation. Population Standard Deviation =
calculate the mean, median, mode, and standard deviation based on the data provided. Response # Q3:...
calculate the mean, median, mode, and standard deviation based on the data provided. Response # Q3: How many times have you called the police in the last year? 1 0 2 0 3 0 4 2 5 4 6 9 7 0 8 2 9 5 10 4 11 3 12 0 13 0 14 0 15 1 16 8 17 15 18 7 19 2 20 2 21 4 22 3 23 0 24 0 25 1 26 0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT