Advertisements
Advertisements
Question
Design a class to overload a method series() as follows:
- double series (double n) with one double argument and returns the sum of the series.
sum `1/1 + 1/2 + 1/3 + ................+ 1/n` - double series (double a, double n) with two double arguments and returns the sum of the series.
sum `1/a^2 + 4/a^5 + 7/a^8 + 10/a^11 + .....................` to n terms
Code Writing
Solution
import java.util.*;
class FQ20
{
double series(double n)
{double sum = 0.0;
n = (int) n;
for (int i = 1; i <= n; i++)
sum += 1.00 / {double) i;
return sum;
}
double series(double a, double n)
{double sum = 0.0, x = 1;
int terms = (int) n;
for (int i = 1; i <= terms; i++)
{sum += x / Math.pow(a, x + 1);
x = x + 3;
}
return sum;
}}
shaalaa.com
Is there an error in this question or solution?
Chapter 5: User - Defined Methods - EXERCISES [Page 340]