Design a class to overload a function volume() as follows:
(i)double volume(double r) – with radius ‘r’ as an argument,
returns the volume of sphere using the formula: v = 4 / 3 × 22 / 7 × r3
(ii)double volume(double h, double r) – with height ‘h’ and radius ‘r’ as the arguments,
returns the volume of a cylinder using the formula: v = 22 / 7 × r2 × h
(iii) double volume(double l, double b, double h) –
with length ‘l’, breadth ‘b’ and height ‘h’ as the arguments,
returns the volume of a cuboid using the formula: v = l × b × h
Program
class Area
{
double volume(double r)
{
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r)
{
double vol = 22/ (double)7 * Math.pow(r, 2) * h;
return vol;
}
double volume(double l, double b, double h)
{
double vol = l * b * h;
return vol;
}
public static void main(String args[])
{
Area ob = new Area();
System.out.println(ob.volume(3.0));
System.out.println(ob.volume(5.0,8.0));
System.out.println(ob.volume(3.0,3.2,2.0));
}
}
{
double volume(double r)
{
double vol = 4.0 / 3 * 22 / 7 * Math.pow(r, 3);
return vol;
}
double volume(double h, double r)
{
double vol = 22/ (double)7 * Math.pow(r, 2) * h;
return vol;
}
double volume(double l, double b, double h)
{
double vol = l * b * h;
return vol;
}
public static void main(String args[])
{
Area ob = new Area();
System.out.println(ob.volume(3.0));
System.out.println(ob.volume(5.0,8.0));
System.out.println(ob.volume(3.0,3.2,2.0));
}
}
No comments:
Post a Comment