Method Overloading
Method Overloading
1
1. Design a class overload a function area( ) 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
R
the formula.
Area = 1 height(a + b)
2
TE
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a
rhombus using the formula:
Area = 1 (diagonal1 x diagonal2)
2
2. Design a class finder with following overloaded functions:
AS(i) int findMin(int a, int b) – to decides and return the smallest from a and b. if both the numbers are same
then return 0.
(ii) int findMin(int a, int b,int c) – to decides and return the smallest from a, b and c. If all the numbers are
same then return 0.
Write main function to input three numbers a ,b,c and by invoking above functions. Print smallest from a
EM
and b and smallest in a, b and c.
3. Design a class Area to find the area of following geometrical figures to illustrate function overloading.
invoke them.
● Area of circle = π𝑟2
● Area of square = a2
● Area of rectangle = length * breadth
OR
●
4. Design a class with the name volume using function overloading that computes the volume of cube, sphere
and a cuboid.
Formula: Volume of cube(vc) = s * s * s
Volume of sphere(vs) = 4 * π * r3
Volume of cuboid(vcd) = l * b * h
sC
6. Design a class with the name Perimeter using function overloading that computes the perimeter of a square, a
rectangle and a circle.
Formula: Perimeter of a square = 4*s
Perimeter of rectangle = 2*(l+b)
Perimeter of a circle = 2* π*r
R
(i) void compare(int, int) : to compare two integer values and print the greater of the two integers.
(ii) void compare(char, char) : to compare the numeric value of two characters and print with the higher
numeric value:
TE
(iii) void compare(String, String) : to compare the length of the two strings and print the longer of the two.
AS 1 2 3 n
(ii) double series(double a, double n) with two double arguments and returns the sum of the series.
sum = 1 + 4 + 7 + 10 + ………………………. To n terms
a2 a5 a8 a11
EM
9. Design a program to overload a method hline()
void hline(int n ) – draw a horizontal line n character long using the character “*” to draw the triangle.
*
***
*****
*******
void hline(int n, char c) – draw a horizontal line n characters long using character stored in c to draw the line.
OR
A
BB
CCC
DDDD
EEEEE
sC
R
12. 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
TE
(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:
AS 1 + 1 + 1 ………………… 1
1 2 3 10
EM
OR
sC