Results 1 to 16 of 16
 02122012, 03:46 AM #1
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:
Java 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 ; } }
Help would be very much appreciated!!
 02122012, 04:01 AM #2
Re: Computing standard deviation using public static classes to call from another cla
I don't know how to call from sum and avg classes into the stdev class
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.
 02122012, 04:07 AM #3
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
Java 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; }
 02122012, 04:17 AM #4
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. ???
 02122012, 04:23 AM #5
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:
xiomaraarreolasmacbook4:csc211 Xio$ java hw6
Please enter a positive integer:6
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)
 02122012, 04:29 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
Re: Computing standard deviation using public static classes to call from another cla
The standard deviation: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
 02122012, 04:32 AM #7
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!
 02122012, 04:33 AM #8
Re: Computing standard deviation using public static classes to call from another cla
at java.io.PrintStream.printf(PrintStream.java:800)
at hw6.main(hw6.java:23)
 02122012, 04:39 AM #9Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
Re: Computing standard deviation using public static classes to call from another cla
Changed it to "%.2f" and now it runs perfectly!
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).
 02122012, 04:53 AM #10
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:
Please enter a positive integer:5
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
Java Code:public static double stdev(int [] A ){ int tot = sum(A); double stdev = Math.sqrt((tot+(A.length*A.length))/A.length); return stdev; }
Just as how I have it in this method:
Java Code:public static double avg(int [] A) { int total = sum(A); double average = (double) total/A.length; return average; }
 02122012, 05:11 AM #11
Re: Computing standard deviation using public static classes to call from another cla
Correction, computing standard deviation is actually done like this
Java Code:public static double stdev(int [] A ){ double variance = ((sum(A) + (A.length*A.length))/A.length); double stdev = Math.sqrt(variance); return stdev; }
Thanks guys!
 02122012, 05:24 AM #12Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
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:
Java 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; } }
Java 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
 02122012, 05:38 AM #13
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!
 02122012, 04:51 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
Re: Computing standard deviation using public static classes to call from another cla
The only person who got everything done by Friday was Robinson Crusoe.
 02132012, 09:56 PM #15
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:
Java 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; }
 02132012, 10:00 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Is it bad practice to make attributes public so other classes can call their methods?
By enerj in forum New To JavaReplies: 4Last Post: 10112011, 11:48 PM 
standard deviation error
By rochla16 in forum New To JavaReplies: 2Last Post: 05032011, 04:43 AM 
standard deviation calculation
By rochla16 in forum New To JavaReplies: 5Last Post: 04282011, 07:27 PM 
Call Static Methods Outside Classes
By Insomniac Riot in forum New To JavaReplies: 4Last Post: 05112010, 10:03 PM 
standard deviation and mean for an array
By peterdfl in forum New To JavaReplies: 3Last Post: 05292008, 04:09 AM
Bookmarks