Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Standard Deviation

 
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));
}
}
 
lowercase baba
Posts: 12893
63
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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic