Computer Applications
Design a class to overload a function series( ) as follows:
(a) void series(int a, int n) — To display the sum of the series given below:
a - (a/2!) + (a/3!) - (a/4!) + …… n terms
(b) void series(int n) — To display the sum of the series given below:
1/2 - 2/3 + 3/4 - 4/5 + …… n terms
Write a main method to create an object and invoke the above methods.
Java
User Defined Methods
63 Likes
Answer
import java.util.Scanner;
public class KboatOverload
{
void series(int a, int n) {
double sum = 0;
int m = 1;
for (int i = 1; i <= n; i++) {
long f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
double t = (double)a / f * m;
sum += t;
m *= -1;
}
System.out.println("Series Sum = " + sum);
}
void series(int n) {
double sum = 0;
int m = 1;
for (int i = 1; i <= n; i++) {
double t = ((double)i / (i + 1)) * m;
sum += t;
m *= -1;
}
System.out.println("Series Sum = " + sum);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
KboatOverload obj = new KboatOverload();
obj.series(a, n);
obj.series(n);
}
}Output

Answered By
9 Likes
Related Questions
Method prototype for the method compute which accepts two integer arguments and returns true/false.
- void compute (int a, int b)
- boolean compute (int a, int b)
- Boolean compute (int a,b)
- int compute (int a, int b)
Define a class to overload the method perform as follows:
double perform (double r, double h) — to calculate and return the value of curved surface area of cone
void perform (int r, int c) — Use NESTED FOR LOOP to generate the following format
r = 4, c = 5
output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5void perform (int m, int n, char ch) — to print the quotient of the division of m and n if ch is Q else print the remainder of the division of m and n if ch is R
Consider the following program segment and answer the questions below:
class calculate { int a; double b; calculate() { a=0; b=0.0; } calculate(int x, double y) { a=x; b=y; } void sum() { System.out.println(a*b); }}Name the type of constructors used in the above program segment?