wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Standard Deviation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Standard Deviation" Watch "Standard Deviation" New topic
Author

Standard Deviation

Stephen Barrow
Greenhorn

Joined: Mar 03, 2004
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]);
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));
}
}
Stephen Barrow
Greenhorn

Joined: Mar 03, 2004
Posts: 13
anybody there?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10918
    
  12

if you want to hard code the values, take out the getValues() method, and create your own List with the values you want.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephen Barrow
Greenhorn

Joined: Mar 03, 2004
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

Joined: Oct 02, 2003
Posts: 10918
    
  12

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

Joined: Mar 03, 2004
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

Joined: Mar 03, 2004
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

Joined: Oct 02, 2003
Posts: 10918
    
  12

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

Joined: Aug 20, 2001
Posts: 1815
....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.)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stephen Barrow
Greenhorn

Joined: Mar 03, 2004
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();
values.add(10);
values.add(20);
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[]
values.add(10);
^
java:14: cannot resolve symbol
symbol : method add (int)
location: class double[]
values.add(20);
^
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

Joined: Oct 02, 2003
Posts: 10918
    
  12

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

Joined: Oct 02, 2003
Posts: 10918
    
  12

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...
 
Consider Paul's rocket mass heater.
 
subject: Standard Deviation
 
Similar Threads
Problem while Adding Doubles
Standard Deviation
Generics Doubt
ArrayIndexOutOfBounds help!
Counting in Array List/ Standard deviation