Sunday, 23 September 2012

Volume Of Sphere

Single Inheritance Program To Find The Volume Of A cylinder and a sphere
/* Program To Implement Simple Program On Single Inheritance */
class Sphere
{
double r;

double area()
{
return (r * r * r * 4 * 3.1416 / 3);
}

}

class Cylinder extends Sphere
{
double h;

double area()
{
return 3.1416 * r * r * h;
}

}

class Volume
{
public static void main(String args[])
{
Cylinder C = new Cylinder();
C.r = 2.0;
C.h = 6.0;
double cyl = C.area();

Sphere S = new Sphere();
S.r = 5.0;
double sph = S.area();

System.out.println("Area of sphere is "+sph+" and area of cylinder is "+cyl);
}
}

/* OUTPUT *

Area of sphere is 523.6 and area of cylinder is 75.3984 */

0 comments:

Post a Comment