QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A ICSE2018-B07 #7194

Question:

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 R3
(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 R2 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

Answer:

public class Overload {
double volume(double r) {
double vol = 4.0 / 3 * 22 / 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;
}
}