-1

I have an assignment for my class that reads like this: Write a class called Stats. The constructor will take no input. There will be a method addData(double a) which will be used to add a value from the test program. Methods getCount(), getAverage() and getStandardDeviation() will return the appropriate values as doubles.

Here's what I have so far:

public class Stats
{
public Stats (double a)
{

a=0.0
}

    public void addData(double a)
    {
    while (
    sum=sum+a;
    sumsq=sumsq+Math.pow(a,2)
    count=count+1
    }

    public double getCount()
    {

    return count;
    }

    public double getAverage()
    {

    average=sum/count
    return average;
    }

    public double getStandardDeviation()
    {


private double sum=o;
private double count=0;
private double sumsq=0;

My problem is figuring out how to calculate the standard deviation using the variables I've defined.

Thanks guys!

1 Answers1

0

You can't do this with the variables you defined. You need to keep the original data to be able to compute the formula

sigma = Math.sqrt( sum(Math.pow(x-mean, 2)) / count )

So,

(1) create private array or list into which you'll add your values in addData. That's all you need to do in addData.

(2) getCount = length of the list

(3) getAverage = sum of values in list / getCount()

(4) getStandardDeviation is something like

double avg = getAverage();
double cnt = getCount();
double sumsq = 0;
for (int i = 0; i < values.Count(); i++) {
   sumsq += Math.pow(values[i] - avg, 2);
}
stdev = Math.sqrt(sumsq / cnt);
kgu87
  • 2,050
  • 14
  • 12