• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Standard Deviation

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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));
}
}
 
Stephen Barrow
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody there?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic