Computer Applications
Design a class to overload a function sumSeries() as follows: (i) void sumSeries(int n, double x): with one integer argument and one double argument to find and display the sum of the series given below: (ii) void sumSeries(): to find and display the sum of the following series:
Answer
public class KboatOverload
{
void sumSeries(int n, double x) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double t = x / i;
if (i % 2 == 0)
sum -= t;
else
sum += t;
}
System.out.println("Sum = " + sum);
}
void sumSeries() {
long sum = 0, term = 1;
for (int i = 1; i <= 20; i++) {
term *= i;
sum += term;
}
System.out.println("Sum=" + sum);
}
}Output
Related Questions
Invoking a method by passing the objects of a class is termed as:
- Call by reference
- Call by value
- Call by method
- Call by constructor
Assertion (A): Static method can access static and instance variables.
Reason (R): Static variable can be accessed only by static method.
- Assertion and Reason both are correct.
- Assertion is true and Reason is false.
- Assertion is false and Reason is true.
- Assertion and Reason both are false.