# Computing standard deviation using public static classes to call from another class

• 02-12-2012, 03:46 AM
exeye0h
Computing standard deviation using public static classes to call from another class
I understand that the formula to computing the standard deviation is something like the following:
n = number of elements in a list
average = sum / n ,
variant = (sum + n*n)/n,
standard deviation = sqrt(variant)
I am assuming that is correct.
So my trouble lies with creating a public static class (or method or function, I can't seem to remember but I probably should know what it actually is!) that calls from another p.s. that already calculated the average and sum.
This is my code thus far:
Code:

```import java.util.Scanner; import java.util.Random; class hw6{                 public static void main(String[] args) {                 int n;                                 Scanner sc = new Scanner(System.in) ;                                         System.out.print("\n\n\tPlease enter a positive integer:") ;                                                 n = sc.nextInt();                         int [] arr = new int[n];                         System.out.printf("\tBefore calling the method gen() \n");                         print(arr);                         gen (arr, n) ;                         System.out.printf("\tAfter calling the method gen() \n");                         print(arr);                         System.out.printf("\n\tThe number of data is: %d \n", n);                         System.out.printf("\n\tThe largest element is: %d \n", max(arr));                         System.out.printf("\n\tThe smallest element is: %d \n", min(arr));                         System.out.printf("\n\tThe average is : %.2f\n", avg(arr));                         System.out.printf("\n\tThe standard deviation: %d \n", stdev(arr));                         histogram(arr,100);         }                         public static int sum(int [] A){                 int total = 0;                 for (int i = 0; i < A.length ; i++)                         total += A[i];                 return total;         }                 public static double avg(int [] A) {                 int total = sum(A);                 double average =  (double) total/A.length;                 return average;                 }                 public static int max(int [] A ) {                 int m = A[0] ;                 for (int i = 1 ; i < A.length; i++)                         if (A[i] > m) m = A[i];                 return m;         }                 public static int min(int [] A ) {                 int n = A[0] ;                 for (int i = 1 ; i < A.length; i++)                         if (A[i] < n) n = A[i];                 return n;         }                 public static double stdev(int [] A ){                 // i am gonna need to call the sum                 // and the average                 int tot = sum(A);                 double stdev = Math.sqrt(tot/A.length);                 return stdev;         }                        public static void gen(int [] A, int n){                         // this method will generate n random integer                         // and store them in the array A                 Random rand = new Random() ;                                 for (int i = 0; i < n ; i++ )                         A[i] = rand.nextInt(101) ;         } // end of gen                 public static void print(int [] B) {                 for (int i = 0; i < B.length ; i++){                         if ( i % 10 == 0 ) System.out.printf("%5d", B[i]);                         else System.out.printf( "%5d", B[i]);         }                 System.out.printf( "\n\n");         }                 public static void histogram( int [] arr, int max) {                 int [] count = new int[max+1];                 for (int i = 0; i < arr.length ; i++)                         count[arr[i]]++;                 for (int i = 0; i < count.length; i++)                         if (count[i] != 0){                         //        printStar(count[i]);                         //        System.out.println();                                                                 System.out.printf("%3d:%s\n", i, printStar(count[i]));                         }                                                         }                         public static String printStar(int n) {                 String line = "" ;                 for (int i = 0; i < n; i++)                         line +="*";                 return line ;         } }```
I don't know how to call from sum and avg classes into the stdev class to produce the standard deviation!
Help would be very much appreciated!!
• 02-12-2012, 04:01 AM
Norm
Re: Computing standard deviation using public static classes to call from another cla
Quote:

I don't know how to call from sum and avg classes into the stdev class
I assume you mean methods not classes.
Not sure what you mean by "call into"?
Are you trying to get at a variable that is defined inside of a method from another method? That's not possible.
You can call any method of a class from any other method of a class.
• 02-12-2012, 04:07 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
Yes, methods*. Apologies!
When I say "call into", I'm referring to instead of recomputing the sum and the average inside of the method "public static stdev(int [] A){ ... } "
because I already computed these in
Code:

```        public static int sum(int [] A){                 int total = 0;                 for (int i = 0; i < A.length ; i++)                         total += A[i];                 return total;         }                 public static double avg(int [] A) {                 int total = sum(A);                 double average =  (double) total/A.length;                 return average;                 }```
how do I compute the standard deviation without rewriting the computation for sum and average within the stdev method since I have the sum and average methods ready to go?
• 02-12-2012, 04:17 AM
Norm
Re: Computing standard deviation using public static classes to call from another cla
Can you call those two methods when you need their results?
The stddev() method currently calls the sum() method. ???
• 02-12-2012, 04:23 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
The stdev part isn't working correctly because I get an error message when I attempt to run it.
I do know that the sum and average methods do work correctly because I ran them before I implemented the stdev method.
This is what occurred in my Terminal when I attempted to run this code:

xiomara-arreolas-macbook-4:csc211 Xio\$ java hw6

Before calling the method gen()
0 0 0 0 0 0

After calling the method gen()
82 27 57 7 24 78

The number of data is: 6

The largest element is: 82

The smallest element is: 7

The average is : 45.83

The standard deviation: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter\$FormatSpecifier.failConversion (Formatter.java:3978)
at java.util.Formatter\$FormatSpecifier.printInteger(F ormatter.java:2705)
at java.util.Formatter\$FormatSpecifier.print(Formatte r.java:2658)
at java.util.Formatter.format(Formatter.java:2430)
at java.io.PrintStream.format(PrintStream.java:899)
at java.io.PrintStream.printf(PrintStream.java:800)
at hw6.main(hw6.java:23)
• 02-12-2012, 04:29 AM
pbrockway2
Re: Computing standard deviation using public static classes to call from another cla
Quote:

The standard deviation: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
d is for decimal integer, but you are trying to use it with the std deviation which is a floating point value.
• 02-12-2012, 04:32 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
pbrockway2, you always come in to save my Java day(s)!
Thank you very much, that was my precise error.
Changed it to "%.2f" and now it runs perfectly!
• 02-12-2012, 04:33 AM
Norm
Re: Computing standard deviation using public static classes to call from another cla
Quote:

at java.io.PrintStream.printf(PrintStream.java:800)
at hw6.main(hw6.java:23)
The problem is on line 23 where you are calling the printf() method using the value returned by the stddev() method.
• 02-12-2012, 04:39 AM
pbrockway2
Re: Computing standard deviation using public static classes to call from another cla
Quote:

Changed it to "%.2f" and now it runs perfectly!
Umm. I dunno. How have you tested it?

Because it generates a whole bunch of ints randomly it may be hard to see if all is as well as it might be. If it were me I would temporarily alter gen() slightly so that it always returned the same value. (because I know the standard deviation in this case is zero).
• 02-12-2012, 04:53 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
Well I had to correct my standard deviation method that I had posted initially because that was computing the incorrect stdev.
And as I tested it, I entered a small integer, 5, and did the computations by hand (err.. calculator*) and compared the manual calculations with the code's calculations.
I got a different answer from my code's but they're not too far off from each other.
This is what my code calculated:

The number of data is: 5

The largest element is: 92

The smallest element is: 6

The average is : 45.80

The standard deviation: 7.07
6:*
16:*
52:*
63:*
92:*

I got 7.1274118... for the standard deviation.
So not too far off. It could probably be that
Code:

```        public static double stdev(int [] A ){                 int tot = sum(A);                 double stdev = Math.sqrt((tot+(A.length*A.length))/A.length);                 return stdev;         }```
where I am computing the Math.sqrt(...) I am forgetting a (double) in there, possibly?
Just as how I have it in this method:
Code:

```        public static double avg(int [] A) {                 int total = sum(A);                 double average =  (double) total/A.length;                 return average;                 }```
• 02-12-2012, 05:11 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
Correction, computing standard deviation is actually done like this
Code:

```        public static double stdev(int [] A ){                 double variance = ((sum(A) + (A.length*A.length))/A.length);                 double stdev = Math.sqrt(variance);                 return stdev;         }```
So I believe I have done everything correctly.
Thanks guys!
• 02-12-2012, 05:24 AM
pbrockway2
Re: Computing standard deviation using public static classes to call from another cla
Unless I'm missing something there is no difference between the two versions of stddev you have posted.

Is there some reason why you didn't try your method on an array filled with the same value? (Including an array of length 1)

This is what I see:

Code:

```import java.util.Arrays; public class StdDevEg {     public static void main(String[] args) {         int[] data = {1, 1, 1};         System.out.println(Arrays.toString(data));         System.out.println("Standard deviation is zero");         System.out.println("stdev() gives the value " + stdev(data));         System.out.println("cast to (double) before division gives " + stdev2(data));                 data = new int[]{1};         System.out.println(Arrays.toString(data));         System.out.println("Really simple case sd=0");         System.out.println("stdev() gives the value " + stdev(data));         System.out.println("cast to (double) before division gives " + stdev2(data));         data = new int[]{2, 4, 4, 4, 5, 5, 7, 9};         System.out.println(Arrays.toString(data));         System.out.println("Standard deviation is 2 (according to Wikipedia)");         System.out.println("stdev() gives the value " + stdev(data));         System.out.println("cast to (double) before division gives " + stdev2(data));         data = new int[]{1, 2};         System.out.println(Arrays.toString(data));         System.out.println("Standard deviation is ???");         System.out.println("stdev() gives the value " + stdev(data));         System.out.println("cast to (double) before division gives " + stdev2(data));     }         public static int sum(int [] A){         int total = 0;         for (int i = 0; i < A.length ; i++)             total += A[i];         return total;     }         public static double stdev(int [] A ){         double variance = ((sum(A) + (A.length*A.length))/A.length);         double stdev = Math.sqrt(variance);         return stdev;     }     public static double stdev2(int [] A ){         double variance = ((double)(sum(A) + (A.length*A.length))/A.length);         double stdev = Math.sqrt(variance);         return stdev;     } }```
The output is

Code:

```[1, 1, 1] Standard deviation is zero stdev() gives the value 2.0 cast to (double) before division gives 2.0 [1] Really simple case sd=0 stdev() gives the value 1.4142135623730951 cast to (double) before division gives 1.4142135623730951 [2, 4, 4, 4, 5, 5, 7, 9] Standard deviation is 2 (according to Wikipedia) stdev() gives the value 3.605551275463989 cast to (double) before division gives 3.605551275463989 [1, 2] Standard deviation is ??? stdev() gives the value 1.7320508075688772 cast to (double) before division gives 1.8708286933869707```
The last case shows that a cast to double really does make a difference and you should decide whether or not it should be there. The first three cases show that something is awry with the assumption you made in the original post. I would check with Standard deviation - Wikipedia, the free encyclopedia
• 02-12-2012, 05:38 AM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
In this case, I will have to consult with my professor because I more or less took the idea from his example in one of my lectures. I will find the solution!
• 02-12-2012, 04:51 PM
JosAH
Re: Computing standard deviation using public static classes to call from another cla
Quote:

Originally Posted by exeye0h
I understand that the formula to computing the standard deviation is something like the following:
n = number of elements in a list
average = sum / n ,
variant = (sum + n*n)/n,
standard deviation = sqrt(variant)
I am assuming that is correct.

I don't think it is correctl; your formula for the 'variant' is equivalent to avg+n and that doesn't make sense.

kind regards,

Jos
• 02-13-2012, 09:56 PM
exeye0h
Re: Computing standard deviation using public static classes to call from another cla
JosAH, yes my equation was incorrect. I had spent the better part of half a day trying to correct it.
This is the correct method:

Code:

```        public static double stdev(int [] A ){                 int sumofSq = 0;                 double vari = 0;                 for (int i = 0; i < A.length ; i++)                         sumofSq += A[i]*A[i];                         vari = (double) sumofSq/A.length - avg(A)*avg(A);                 double stdev = (double) Math.sqrt(vari);                 return stdev;         }```
• 02-13-2012, 10:00 PM
JosAH
Re: Computing standard deviation using public static classes to call from another cla
Yep, that one is correct; as you can see from your own code, you don't even need an array, i.e. keeping track of the sum of the numbers, the sum of the square of the numbers and n is enough to produce the sum, average and standard deviation. That's the way those old calculators did it (they didn't even have enough memory for arrays).

kind regards,

Jos