This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Standard Deviation

 
Stephen Barrow
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
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]);
values.add(value);
}
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));
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
There's no need to post this to multiple forums. most people just find it annoying, and it makes it harder to figure out where to post an answer.
 
    Bookmark Topic Watch Topic
  • New Topic