Results 1 to 17 of 17
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Using method to compute the sum of n numbers, not getting the return value

    m(i) = i / ( i + 1) where i goes from 1 to 20

    The program is supposed to show a table with i and the corresponding value m(i) listed.
    i.e.

    i m(i)
    _____________
    1 0.5000
    2 1.1667
    .
    .
    .
    .
    20 .....

    I got my code to compile but I am getting the same value for all values of i (1 to 10).

    Java Code:
    public class HW5_13New
    {
      public static double sum(int i)
      {
        double add = 0;
       
        for ( i = 1; i <= 20; i++ )
        {
          add = (double)i / ( i + 1);
          
        }
         
         return add;
      }
      
      public static void main(String[] args)
      {
           
        System.out.println("i          m(i)");
        System.out.println("____________________");
    
        for ( int i = 1; i <= 20; i++)
        {
         double ans = sum (i);
         
         System.out.print("\n" + i + " " +"         " + ans);
        }
      }
     
    }
    Last edited by abi; 02-28-2013 at 02:31 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Java Code:
    public static int sum(int i)
    You have declared sum() to return an int. But in fact you intend it to return a floating point number (a double). So it should be declared to return a double.

    In code like this you need to give some thought to what sort of thing (int or double) you intend each variable be.

    I am having trouble taking the value from the Sum method into the main method.
    It would help to describe the problem. Does what you posted compile? Does it compile and run but print strange values at runtime? In the latter case, observe that i/(i+1) is always zero when i is an int. You can get a double answer by using a cast: (double)i/(i+1)

  3. #3
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    I had been completely oblivious of that. Just didn't occur to me. Rookie mistakes I guess. So I made that correction all of my variables are doubles now.

    I guess I didn't describe my problem adequately enough. Yes it does compile, that is what i meant when I said it displays. After I made the variable type correction it shows 0.5 for all the i (1 - 20).

    Thanks man.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Looks like you simply keep assigning a new value to add. So the only value saved is the last one in the loop.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    I don't see a way to get around that. Could you tell me how I could store the value of each i to add?

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Here is your sum method:

    Java Code:
    public static double sum(int i)  {
        double add = 0;
    
        
        for ( i = 1; i <= 20; i++ )
        {
          add = (double)i / ( i + 1);
           
        }
          
    
         return add;
    
      }
    First, you pass a value to i but then you don't use it because you set i to 1 in the for loop.
    To update add, do the following:

    Java Code:
    add = add + (double)i/(i+1);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    I'm glad you've got the double/int thing sorted out.

    Jim's right, you've got to "accumulate" the result. You can also use += for that.

    Is there some reason you're not doing the whole thing in main()? I'm a fan of using separate methods, but here you are calculating the same thing over and over again.

  8. #8
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    I am learning to use separate methods and the assignment requires that I use it.
    Wouldn't that result in a total sum.

    I want this:
    say it first takes i = 1 and comes out with 0.5
    so add = 0.5
    it displays 1 0.5
    then
    now it goes to i = 2 and gets 0.6667
    so wouldnt add = 0.5 + 0.0667
    and so on until it reaches 20
    Last edited by abi; 02-28-2013 at 05:30 AM.

  9. #9
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Couldn't that result in a total sum.
    I want this:
    say it first takes i = 1 and comes out with 0.5
    so add = 0.5
    it displays 1 0.5
    then
    now it goes to i = 2 and gets 0.6667
    so wouldnt add = 0.5 + 0.0667
    and so on until it reaches 20
    Last edited by abi; 02-28-2013 at 05:30 AM.

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Ok, perhaps you don't want to update "add." I reread the first thread. The loop in the sum method confused me. You don't need it.

    Just simply return the value i/(i+1) from the sum method. The reason your method is messing up is because the i in the for loop is clobbering your
    method argument. And since you aren't really summing anything you might want to change the name (but it isn't necessary for operation).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Java Code:
    public class HW5_13New
    {
      public static double sum(int i)
      {
       
         double add += (double)i / ( i + 1);
         
       } 
         
         return add;
       
      }
      
      public static void main(String[] args)
      {
           
        System.out.println("i          m(i)");
        System.out.println("____________________");
    
        for ( int i = 1; i <= 20; i++)
        {
         double ans = sum (i);
         
         System.out.print("\n" + i + " " +"         " + ans);
        }
      }
    This results in the program giving me individual values for the values of i
    sample
    i m(i)
    1 0.5
    2 0.6667 <---- 0.5 + 0.6667
    .
    .
    so on

    i need
    1 0.5
    2 1.667
    ...

    I tried a few things with this updated code but it isnt happening.

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    OK, let me try and understand. You want to create a list of values as follows:

    i i/(i+1)

    1 1/2
    2 2/3
    3 4/5
    ...
    ...
    19 19/20
    20 20/21


    Am I correct? If so check out my post #10
    Jim
    Last edited by jim829; 02-28-2013 at 05:58 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Sorry for being unclear.

    No that is not correct but that is what I am getting currently.

    What I need is for it to add up as it goes from 1 - 20 and display the sum as it goes along

    i i/(i+1)

    1 1/2
    2 (1/2)+ (2/3)
    3 (1/2)+ (2/3) + (3/4)
    ...

  14. #14
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    OK, got it. What you need to to is sum of the values in add as we have told you. But you want to increase the number of terms you are summing for each call. This is controlled by the termination for value n which is now passed to sum. If you want to include the nth iteration in sum change to from < n to <= n.

    Java Code:
    public static double sum(int n)  {
          double add = 0;
    
    
        for (int i = 1; i < n; i++) {
         add += (double)i / ( i + 1);
        }
        return add
    }
    Next, in the other for loop, you pass the index to sum. Thus, each time sum is called in will calculate one more element.
    Now you can play with it to fine tune it to the proper output.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Ok I got that and came up with this. The only problem now is that it displays
    1 0.0
    2 0.5
    3 1.16
    ...
    do you see the trend?the values on the right have been pushed down per say.I poked at it but i can't see how to get it to start at 1 - 0.5.

    Java Code:
    public class HW5_13New
    {
      public static double sum(int n)
      {
        double add = 0;
       
         for (int i = 1; i < n; i++)
         {
         add += (double)i / ( i + 1);
         }
         
         return add;
       
      }
      
      public static void main(String[] args)
      {
           
        System.out.println("i          m(i)");
        System.out.println("____________________");
    
        for ( int n = 1; n <= 20; n++)
        {
         sum (n);
         
         System.out.print("\n" + n + " " +"         " + sum(n));
        }
      }
     
    }

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Try changing the loop comparison in the sum method from "<" to "<=".

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Using method to compute the sum of n numbers, not getting the return value

    Turns out we didn't need to use different methods so I did it within the main method which was pretty easy.
    Thanks

Similar Threads

  1. What can a method return?
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 01-29-2012, 03:22 PM
  2. cant return value from a method
    By gedas in forum New To Java
    Replies: 2
    Last Post: 03-23-2011, 08:37 AM
  3. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 AM

Posting Permissions

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