# Standard Deviation

Stephen Barrow
Greenhorn
Posts: 13
Can somebody tell me how I can change this code to allow me to hard code the values into it? such as double [] values = {10,20,30,4,2};
import java.util.*;
public class Deviator
{
public static void main(String [] args)
{

Deviator deviator = new Deviator();
List values = deviator.getValues(args);
double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values);
System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);

System.out.println("No arguments");

}
public List getValues(final String [] args)
{
List values = new ArrayList();
for (int i = 0; i < args.length; ++i)
{
try
{
Double value = Double.valueOf(args[i]);
}
catch (NumberFormatException e)
{
System.err.println("Not a number: " + args[i]);
}
}
return values;
}

public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}

Stephen Barrow
Greenhorn
Posts: 13
anybody there?

fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
if you want to hard code the values, take out the getValues() method, and create your own List with the values you want.

Stephen Barrow
Greenhorn
Posts: 13
Ive tried taking it out but I can't get the code to run right. I keep getting cannot resolve errors etc..

fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Post what you've tried doing, and post the exact error message. also, it helps if you use the "code" tags around your code. it preserves formatting and makes it easier to read.

Stephen Barrow
Greenhorn
Posts: 13
import java.util.*;
public class Deviator
{
public static void main(String [] args)
{
Deviator deviator = new Deviator();
double [] values = {10,20,30,4,2}; //This is were I define my array

double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values); System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);
}

public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}

Stephen Barrow
Greenhorn
Posts: 13
Error messages that result when the above code is compliled
java:12: calculateMean(java.util.List) in Deviator cannot be applied to (double[])
double mean = deviator.calculateMean(values);

java:13: calculateStandardDeviation(java.util.List) in Deviator cannot be applied to (double[])
double stddev = deviator.calculateStandardDeviation(values);
^

fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
ok, you've created an Array with the data you want. you then call the calculateMean() method.
but if you look at it's signiature:

it wants a List object, not an Array object. you need to get those values into a List.
in place of your array definition, you could either write several statements like

or, you could write a method that you pass an array into, that then parses the array (using a loop) and puts each element into the List. That method would then return the list. it would be VERY similar to the getValues method that already exists.

Joel McNary
Bartender
Posts: 1840
....or, instead of writing such a method, use the java.util.Arrays.toList() method that already exists.
(Of course, that would expect Objects, not primitive values, so you might be better off writing your own that deals with primitive arrays. So while my suggestion turns out to be not helpful in this situation, there are others in which it may be.)

Stephen Barrow
Greenhorn
Posts: 13
I tried to add your suggested code and heres what I got
import java.util.*;
import java.text.*;
import javax.swing.*;
public class Deviator
{
public static void main(String [] args)
{

Deviator deviator = new Deviator();
double [] values = {10,20,30,4,2}; // This is were I define my array
List values = new ArrayList();
double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values);
System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);
}

public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}
Errors 5:
java:12: values is already defined in main(java.lang.String[])
List values = new ArrayList();
java:13: cannot resolve symbol
symbol : method add (int)
location: class double[]
^
java:14: cannot resolve symbol
symbol : method add (int)
location: class double[]
^
java:15: calculateMean(java.util.List) in Deviator cannot be applied to (double[])
double mean = deviator.calculateMean(values);
java:16: calculateStandardDeviation(java.util.List) in Deviator cannot be applied to (double[])
double stddev = deviator.calculateStandardDeviation(values);

fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Please learn how to use the code tags...
Now, Some of these are my fault... sorry...
first, you can't have an Array called values and a List called values. so, take out the Array.
now, the other errors are my fault. that's what i get for being lazy (and trying to do this at work).
we can't add a primitive to the list... only objects. so, you need to cast the primitive values to their wrapper types. and since later we are expecting to get Doubles out of the list, we better make them Doubles when we put them in.

do this for every value you want in the list, and it should work. i just got some output of

values: [10.0, 20.0]
mean : 15.0
stddev: 7.0710678118654755

you need to add all the values you want to use.

fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
OH, and one other thing. this is really not a good way of doing this. really, inputting the data from the command line makes the most sense to me, but this will work.
if i had to do it this way, writing a method really makes the most sense, and passing it the array with the primitives would work just fine. then, if you wanted to add/delete/change any of the values, you would just have to change the initialization of the array.
I just hope the Java Gods don't strike me down for giving you such a hack...