Design a class to overload a function Sum( ) as follows: (i) int Sum(int A, int B) – with two integer arguments (A and B) calculate and return sum of all the odd numbers in the range of A and B. Sample input: A=5 and B=16 Sample output: sum = 5+7+9+11+13+15= 60 (ii) void Sum( double N, double P, double Q ) – with three double arguments, calculate and print the average of all three double arguments N,P,Q . (iii) void Sum(int N) - with one integer argument (N) calculate and print sum of only even digits of the number N. Sample input : N=45764 Sample output : sum = 4+6+4 = 14 Write the main method to create an object and invoke the above methods.
Answers
Answer:
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
BlueJ output of 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:
BlueJ output of 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:
Explanation:
HOPE IT HELPS YOU DEAR ☺️