Computer Applications
Design a class to overload a function series( ) as follows:
(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + ………. xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 ………. p terms
(c) void series () – To display the sum of the series given below:
1/2 + 1/3 + 1/4 + ………. 1/10
Java
User Defined Methods
ICSE 2019
152 Likes
Answer
public class KboatOverloadSeries
{
void series(int x, int n) {
long sum = 0;
for (int i = 1; i <= n; i++) {
sum += Math.pow(x, i);
}
System.out.println("Sum = " + sum);
}
void series(int p) {
for (int i = 1; i <= p; i++) {
int term = (int)(Math.pow(i, 3) - 1);
System.out.print(term + " ");
}
System.out.println();
}
void series() {
double sum = 0.0;
for (int i = 2; i <= 10; i++) {
sum += 1.0 / i;
}
System.out.println("Sum = " + sum);
}
}
Output



Answered By
56 Likes
Related Questions
Define a class to overload the method display() as follows:
void display(): To print the following format using nested loop.
1 2 1 2 1
1 2 1 2 1
1 2 1 2 1void display (int n, int m) : To print the quotient of the division of m and n if m is greater than n otherwise print the sum of twice n and thrice m.
double display (double a, double b, double c) — to print the value of z where
Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters
- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Using switch statement write a menu driven program to overload a function series as follows:
(a) void series() — To find and display the sum of the following series:
S = 2 - 4 + 6 - 8 + 10 ….. - 20(b) void series(int n, int x) — To find and display the sum of the following series:
S = (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + ……. to n termsAssertion (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.