Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default Problem solved...thanks Jos!!!

    Hi all,

    I've decided to give it another go and here is my updated code...i think i have method m being called right, but my math within method m is wrong, i can't figure out how to make it right...any help would be greatly appreciated...


    /** This program uses a method
    * that calculates 1/2 + 2/3 + ... + i/(i+1)
    */

    public class Assignment4 {
    /* main method */
    public static void main(String[] args) {
    double mi; //used to catch the return value from method m(i)
    //Print the header
    System.out.println("i\t\tm(i)\n");

    //Print the table
    for (int i = 1; i <= 20;i++) {
    mi = m(i); //Fill in blank here. Call the method
    System.out.println(i+"\t\t"+ mi);
    //System.out.println(mi);
    } //end for

    } //end main

    /* define non-void method m(i) here */
    public static double m(double key){
    double sum;
    double i=1;
    key = (i/(i+1));
    sum =+ key + (i/(i+1));
    i++;
    //System.out.println(key);
    return key;
    }
    }

    //end class
    Last edited by mnki23; 11-04-2009 at 04:54 PM. Reason: Change my wording...

  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

    What's the output you're getting? Try putting a print statement inside your m method to test if it's actually being called but maybe not calculating something right.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default Here is the output i am getting

    i m(i)

    1 0.5
    2 0.6666666666666666
    3 0.75
    4 0.8
    5 0.8333333333333334
    6 0.8571428571428571
    7 0.875
    8 0.8888888888888888
    9 0.9
    10 0.9090909090909091
    11 0.9166666666666666
    12 0.9230769230769231
    13 0.9285714285714286
    14 0.9333333333333333
    15 0.9375
    16 0.9411764705882353
    17 0.9444444444444444
    18 0.9473684210526315
    19 0.95
    20 0.9523809523809523

    but the output i'm looking for is:

    i m(i)
    1 0.5
    2 1.1667

    19 16.4023
    20 17.3546

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    i m(i)

    1 0.5
    2 0.6666666666666666
    3 0.75
    4 0.8
    5 0.8333333333333334
    6 0.8571428571428571
    7 0.875
    8 0.8888888888888888
    9 0.9
    10 0.9090909090909091
    11 0.9166666666666666
    12 0.9230769230769231
    13 0.9285714285714286
    14 0.9333333333333333
    15 0.9375
    16 0.9411764705882353
    17 0.9444444444444444
    18 0.9473684210526315
    19 0.95
    20 0.9523809523809523

    but the output i'm looking for is:

    i m(i)
    1 0.5
    2 1.1667

    19 16.4023
    20 17.3546
    I can understand the output you're getting (i/(i+1)), but why do you expect otherwise? I can't see a regularity in your wanted output.

    kind regards,

    Jos

  5. #5
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    but the output i'm looking for is:

    i m(i)
    1 0.5
    2 1.1667

    19 16.4023
    20 17.3546

    According to the book, the correct output should be what i have posted above, can't figure out where i am wrong.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    but the output i'm looking for is:

    i m(i)
    1 0.5
    2 1.1667

    19 16.4023
    20 17.3546

    According to the book, the correct output should be what i have posted above, can't figure out where i am wrong.
    Does you book mention a formula? I'm sure it's not i/(i+1) for several values of i.

    kind regards,

    Jos

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

    Default

    you're dividing a double by a double without any sort of decimal format attached to it. It's going to print out to the furthest decimal it can.

    Also your math may be wrong. If you put in 1 as your starting you get key = 1/(1+1) = 1/2. Your algorithm is decreasing your input so the things you add together get progressively smaller. Picture it as a limit function, your answer will go on to infinity but the each time you're adding something smaller and smaller so you never get to one, you just become infinitely close to it.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  8. #8
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the quick responses....here is the formula the book has

    m(i)=(1/2)+(2/3)+...+i/(i+1)

    regards

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    Thanks for the quick responses....here is the formula the book has

    m(i)=(1/2)+(2/3)+...+i/(i+1)

    regards
    You are not adding your individual terms in your code; your output prints each individual term.

    kind regards,

    Jos

  10. #10
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    I can't figure out how to add them all up....i know that i need to add up (1/2)+(2/3)+(3/4) and so on until you get to (20/21); but i can't figure it out.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    I can't figure out how to add them all up....i know that i need to add up (1/2)+(2/3)+(3/4) and so on until you get to (20/21); but i can't figure it out.
    In the body of your loop add the term m(i) to a variable, say, 'sum'; before you start the loop don't forget to initialize it to 0.

    kind regards,

    Jos

  12. #12
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Gents,

    If one of you could give me a solution it would be greatly appreciated, i've wasted approx. 8 hours on this problem and haven't been able to come up with a solution, i'm so ready to throw in the towel, i'm sure i'm overlooking something simple

  13. #13
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    i've updated my code and am trying again any help is greatly appreciated

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    i've updated my code and am trying again any help is greatly appreciated
    Show your latest version of the code; you are calling method m(i) in your loop and you want to add its return value to a variable over and over again in that loop; when you've initialized that variable to 0.0 you end up with all the values m(1)+m(2)+m(3) ... m(20) in that variable when your loop has finished.

    It's your turn now, use your imagination; if you want to learn how to program you have to use your imagination in a rational way otherwise programming is not for you ...

    kind regards,

    Jos

  15. #15
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Jos,

    Here is the latest code

    public class Assignment4 {
    /* main method */
    public static void main(String[] args) {
    double mi; //used to catch the return value from method m(i)
    //Print the header
    System.out.println("i\t\tm(i)\n");

    //Print the table
    for (int i = 1; i <= 20;i++) {
    mi = m(i); //Fill in blank here. Call the method
    System.out.println(i+"\t\t"+ mi);
    //System.out.println(mi);
    } //end for

    } //end main

    /* define non-void method m(i) here */
    public static double m(double key){
    double sum;
    double i=1;
    key = (i/(i+1));
    sum =+ key + (i/(i+1));
    i++;
    //System.out.println(key);
    return key;
    }


    I've been using my imagination and i'm out of ideas...

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    That mi() method is just rubbish now: you create local variables i and sum at every method call and assign them the same values over and over again. Those local variables are lost when the method termintas.

    Here is the (spoonfed) solution because this thread has been going on for too long already:

    Java Code:
    double sum= 0.0; // contains m(1)+m(2)+ ... m(i)
    for (int i= 1; i <= 20; i++) {
       sum+= m(i); // add the next term to the sum
       System.out.println(i+": "+sum)); // print partial result
    }
    Use the following version of m(int i) for this:

    Java Code:
    double m(int i) {
       return i/(i+1.0);
    }
    kind regards,

    Jos
    Last edited by JosAH; 11-04-2009 at 04:51 PM.

  17. #17
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Jos,

    Thank you for being so patient with me, i greatly appreciate it.

    Regards

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    Jos,

    Thank you for being so patient with me, i greatly appreciate it.

    Regards
    You're welcome of course; you really should show a bit more rational creativity if you want to continue programming; compare my code and your code for an example and try to understand my code and reuse the ideas in it for your further code.

    kind regards,

    Jos

  19. #19
    mnki23 is offline Member
    Join Date
    Nov 2009
    Posts
    11
    Rep Power
    0

    Default

    Yes i've been playing around with your code to see how i can make it work but with different variables and such...I really made it harder then it needed to be.

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mnki23 View Post
    Yes i've been playing around with your code to see how i can make it work but with different variables and such...I really made it harder then it needed to be.
    Yep, I am extremely lazy: do just what has been asked for, no more and no less. b.t.w. try to change the '1.0' to '1' in the m() method (see above) and see what happens and try to understand it; it never hurts to play with the code and temporarily ruin it a bit occasionally ;-)

    kind regards,

    Jos

Page 1 of 2 12 LastLast

Similar Threads

  1. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  2. Need help calling from a different method
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:27 PM
  3. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  4. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 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
  •