Results 1 to 3 of 3
  1. #1
    zlwilly is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Smile 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)));
        }
        
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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)));
    ya typecasting is a pain in the ass. I would always get these errors with working with decimals

    P.S. try it out. 1/5 might still end up being 0.0 but if all else fails just do 0.2*placeholder

  3. #3
    zlwilly is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help! That worked great! I ended up changing the 1/5 to 0.2 and it worked flawlessly.

    I remember my Prof telling us to keep an eye out for such things, but as it wasn't a common occurrence for me I had forgotten all about it. Thanks again!

Similar Threads

  1. Replies: 2
    Last Post: 04-07-2009, 06:25 AM
  2. returning arrays
    By cjohnson412 in forum New To Java
    Replies: 4
    Last Post: 11-25-2008, 01:30 PM
  3. Why is my list returning nothing?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 03:51 PM
  4. Replies: 4
    Last Post: 07-31-2008, 01:43 PM
  5. returning a value from an arraylist
    By xkross in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •