Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: writing a method to add fractions?

1. ## 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!

```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;
}
}```  Reply With Quote

3. ## Re: writing a method to add fractions?

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.  Reply With Quote

4. ## 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  Reply With Quote

5. ## 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?  Reply With Quote

6. ## Re: writing a method to add fractions?  Reply With Quote

7. ## 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?  Reply With Quote

8. ## 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 ---

```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  Reply With Quote

9. ## Re: writing a method to add fractions?

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 ---

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.  Reply With Quote

10. ## Re: writing a method to add fractions?

okay so....
```	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  Reply With Quote

11. ## Re: writing a method to add fractions?

` 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  Reply With Quote