Function Overloading – Volume – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2018-07A #JAVA#7194    siteicon   siteicon  

Problem Statement - Function Overloading – Volume

Design a class to overload a function volume() as follows:

(i) double volume (double R) ¡V with radius (R) as an argument, returns the volume of sphere using the formula.
V = 4/3 x 22/7 x R^3
(ii) double volume (double H, double R) ¡V with height(H) and radius(R) as the arguments, returns the volume of a cylinder using the formula.
V = 22/7 x R^2 x H
(iii) double volume (double L, double B, double H) ¡V with length(L), breadth(B) and Height(H) as the arguments, returns the volume of a cuboid using the formula.
V = L x B x H

Solution

TC++ #7194

public class Overload {
double volume(double r) {
double vol = 4.0 / 3 * 22.0/ 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r) {
double v = 22.0 / 7 * Math.pow(r, 2) * h;
return v;
}
double volume(double l, double b, double h) {
double v = l * b * h;
return v;
}
}

Notes

  • Remember to put 22/7 as 22.0/7 and 4/3 as 4.0/3 so that the calculation can happen in double type and not in int type.


Share

CSKC| Created: 12-Apr-2019 | Updated: 26-Nov-2019|ICSE2018









Back