# writing a method to add fractions?

• April 4th, 2014, 08:39 AM
brobertson300
writing a method to add fractions?
Okay so I have to write a method to compute the following series: m(i)= 1/3 + 2/5 +....+ (i / 2i+1) and write a test program that displays a table " i = m(i)" 1=0.3333 2=0.7333....all the way down to 20 which is 9.2480. I have written something and cannot seem to get the sum of the fractions to display . I could be going about this the completely wrong way but this was my attempt. Please give suggestions!

Code java:

```public class ExtraCredit1 { public static void main(String[] args) { double num; double sum = 1;   for (int i = 1; i <= 20; i++) { System.out.println("" + i + " " + sum + ""); } } public static double m(double i) { double sum = 1; sum += (double) i / (2*i + 1);   return sum; } }```
• April 4th, 2014, 08:44 AM
Norm
Re: writing a method to add fractions?
Quote:

cannot seem to get the sum of the fractions to display
What happens when you compile and execute the code? Copy the output and paste it here. Add some comments to the posted output showing what the output should look like.
• April 4th, 2014, 08:48 AM
brobertson300
Re: writing a method to add fractions?
When I compile it shows the 'i' = 1,2,3,4....20 but the m(i) is 0,0,0,0.... and needs to be 0.3333, 0.7333.... all the way to 9.2480... its supposed to add all the fractions together and display... 1/3 = 0.3333, 1/3 + 2/5 = 0.7333...

Its not supposed to show the actual fraction in the output just;
i m(i)
1 = 0.3333
2 = 0.7333
3
4
• April 4th, 2014, 08:54 AM
Norm
Re: writing a method to add fractions?
What does the program print out when it executes? Can you copy the exact output and not interpret it in text?

BTW m is a poor name for a method. The name of a method should be a verb saying what the method is doing.

Where is the m method called?
• April 4th, 2014, 08:55 AM
brobertson300
Re: writing a method to add fractions?
• April 4th, 2014, 08:57 AM
Norm
Re: writing a method to add fractions?
BTW m is a poor name for a method. The name of a method should be a verb saying what the method is doing.

Where is the m method called?
• April 4th, 2014, 09:05 AM
brobertson300
Re: writing a method to add fractions?
I understand it is a poor name for a method however thats what the teachers instruction are. Do I need to call the method into my println?

--- Update ---

Code java:

```import java.text.DecimalFormat;   public class ExtraCredit1 { public static void main(String[] args) { double num; double sum = 0;   DecimalFormat formatter = new DecimalFormat("0.0000");   for (int i = 1; i <= 20; i++) { System.out.println("" + i + " = " + formatter.format(m(i)) + ""); } } public static double m(double i) { double sum = 0; sum += (double) i / (2*i + 1);   return sum; } }```

--- Update ---

now it displays the fractions but it isnt adding them up after each iteration
• April 4th, 2014, 09:07 AM
Norm
Re: writing a method to add fractions?
Quote:

Do I need to call the method
If a method is NOT called, it does not execute. If it does not execute, it will not compute any value.

--- Update ---

Quote:

it isnt adding them up after each iteration
If you want to accumulate the sum of previous values with the current value, you need to save the sum of the previous values.
• April 4th, 2014, 09:35 AM
brobertson300
Re: writing a method to add fractions?
okay so....
Code java:

``` public static double m(double i) { double sum = 0; sum += (double) (i / (2*i + 1)); total = sum + sum return total; //Will this return the accumulative total? } }```

--- Update ---

no that doesnt work either... hmm
• April 4th, 2014, 09:36 AM
Norm
Re: writing a method to add fractions?
Code :

` total = sum + sum`
Did that code compile? It's better if you compile and execute the code BEFORE posting it.

That looks the same as: total = 2*sum;

Where is the value from the last call to m() saved? That is where the new value for the fraction needs to be added to the old value of sum