Results 1 to 3 of 3
 12022008, 08:29 PM #1Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0
Need help. Method won't returning proper value..
This IS for a school assignment, just so no one bites my head off. I have no interests in plagiarizing other peoples work as my own, I just need some debugging help!
Now that that is out of the way...
I have a class in which numbers in an array are averaged and the standard deviation is calculated. The main part calls the method calcSD(an array, and a double, which is the mean.) I have stepped through the process and I know that the array is being passed to the calcSD method, and the number is being properly calculated. But no matter what I do, even though the correct number is represented in the return statement, I always end up with 0.0 when I try to print out the returned value.
I've looked for general errors, and I'm sure the continued failure of the program is due to my own ignorance. Any help would be GREATLY appreciated. Thanks so much.
(PS. It does compile just fine.)
Java Code:public class finalgrade { public static void main (String[] args) { int i; String[] names = { "Smith", "Harris","Jones","Johnson","Wells"}; double[] scores={90.0, 80.0, 70.0, 60.0, 50.0 }; double mean=calcMean(scores); double SD=calcSD(mean,scores); System.out.println(); System.out.println("The mean is "+mean); System.out.println("The standard deviation is "+SD+"."); System.out.println(); for(i=0;i<scores.length;i++) { System.out.print(names[i]); System.out.println(" "+scores[i]); } } //MUTATORS public static double calcMean(double[] a1) { int i; double swap=0.0; for(i=0;i<a1.length;i++) { swap=swap+a1[i]; } return (swap/a1.length); } public static double calcSD(double m, double[] a2) { int i; double placeholder=0.0; for(i=0;i<a2.length;i++) { placeholder=placeholder+((a2[i]m)*(a2[i]m)); } return (Math.sqrt((1/5)*(placeholder))); } }
 12022008, 08:38 PM #2
your error is logic based from first glance. Math.sqrt((1/5)*(placeholder)); ends up being Math.sqrt((0)*(placeholder)) which is in fact 0.0
If you want 1/5th to == 0.2 I believe you need to do
Java Code:(Double)(Math.sqrt(((Double)(1/5))*(placeholder)));
P.S. try it out. 1/5 might still end up being 0.0 but if all else fails just do 0.2*placeholder
 12022008, 09:44 PM #3Member
 Join Date
 Dec 2008
 Posts
 6
 Rep Power
 0
Similar Threads

File fp = new File(filePath);fp.exists() does not yeild proper result
By ganeshp in forum Advanced JavaReplies: 2Last Post: 04072009, 06:25 AM 
returning arrays
By cjohnson412 in forum New To JavaReplies: 4Last Post: 11252008, 01:30 PM 
Why is my list returning nothing?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11052008, 03:51 PM 
Java Script comes with No proper error message in the Alert box in Struts.
By anandguna in forum Advanced JavaReplies: 4Last Post: 07312008, 01:43 PM 
returning a value from an arraylist
By xkross in forum New To JavaReplies: 2Last Post: 04182008, 05:30 PM
Bookmarks