Results 1 to 20 of 22
Thread: Calling a method
- 11-03-2009, 06:22 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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 classLast edited by mnki23; 11-04-2009 at 04:54 PM. Reason: Change my wording...
- 11-03-2009, 06:36 PM #2
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.
- 11-03-2009, 06:50 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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
- 11-03-2009, 06:54 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
- 11-03-2009, 06:56 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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.
- 11-03-2009, 06:58 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
- 11-03-2009, 07:02 PM #7
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.
- 11-03-2009, 07:04 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
Thanks for the quick responses....here is the formula the book has
m(i)=(1/2)+(2/3)+...+i/(i+1)
regards
- 11-03-2009, 07:15 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
- 11-03-2009, 08:32 PM #10
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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-03-2009, 09:03 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
- 11-04-2009, 03:10 AM #12
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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
- 11-04-2009, 02:24 PM #13
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
i've updated my code and am trying again any help is greatly appreciated
- 11-04-2009, 02:59 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-04-2009, 03:03 PM #15
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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...
- 11-04-2009, 03:12 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
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:
Use the following version of m(int i) for this: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 }
kind regards,Java Code:double m(int i) { return i/(i+1.0); }
JosLast edited by JosAH; 11-04-2009 at 04:51 PM.
- 11-04-2009, 04:15 PM #17
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
Jos,
Thank you for being so patient with me, i greatly appreciate it.
Regards
- 11-04-2009, 04:53 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
- 11-04-2009, 06:08 PM #19
Member
- Join Date
- Nov 2009
- Posts
- 11
- Rep Power
- 0
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.
- 11-04-2009, 06:14 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,416
- Blog Entries
- 7
- Rep Power
- 17
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
Similar Threads
-
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
Need help calling from a different method
By Mayur in forum New To JavaReplies: 6Last Post: 03-08-2009, 09:27 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks