Page 234 - computer science (868) class 11
P. 234
12. A class SeriesSum is designed to calculate the sum of the following series:
x 2 x 4 x 6 x n
Sum = + + + . . .
1! 3! 5! (n-1)!
Some of the members of the class are given below:
Data Members/Instance variables
x : Stores an integer number
n : Stores the number of terms
sum : double variable to store the sum of the series
Member Functions
SeriesSum(int xx, int nn) : Constructor to assign x=xx and n=nn
double find fact(int m) : Returns the factorial of m using the recursive technique
double find power(int x, int y) : Returns x raised to the power of y using the recursive technique
void calculate() : Calculates the sum of the series by invoking the recursive functions respectively
void display() : Displays the sum of the series
Also, define the main() function to create an object and call the functions accordingly to enable the task.
13. Design a class to overload a function compare() with the specifications given as follows:
void compare(int, int) : Compares two integer values and prints the greater of the two integers
void compare (char, char) : Compares the numeric values of two characters and prints the character with a higher numeric
value
void compare (String, String) : Compares the length of the two strings and prints the longer of the two.
14. Design a class to overload a function geometry() with the specifications given as follows:
void geometry(int n, char ch) : With one integer argument and one character argument that draws a filled square of side n
using the character stored in ch
void geometry(int x, int y) : With two integer arguments that draw a filled rectangle of length x and breadth y, using the
symbol ‘a’
void geometry() : With no arguments that draws a filled triangle shown below
For example:
(i) Input value of n = 3, ch = ‘x’
Output: xxx
xxx
xxx
(ii) Input value of x = 3, y = 4
Output: aaaa
aaaa
aaaa
(iii) Output: $
$$$
$$$$$
$$$$$$$
15. Design a class to overload a function area() with the specifications given as follows:
(i) double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
area = √(s(s - a)(s - b)(s - c))
where s = (a + b + c) / 2
(ii) double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area = 1/2 × height × (a + b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
area = 1/2 × (diagonal1 × diagonal2)
16. Using the Overloading function series(), do the following:
(i) int series (int n) : Returns the sum of the series
4
3
5
n
s= 1 + 2 + 3 + 4 + 5 + ……+ n
1
2
(ii) void series (int x, int n) : Prints the sum of the series
s= (x-1)+(x-3) + (x-5) + …….n term
th
(iii) double series (double n) : Prints the following series
1,4,9,16,25,……… n term
th
232232 Touchpad Computer Science-XI

