17) Create a class Shape which consists one final method area () and volume ().Create three subclasses Rect, Circle and Triangle and calculate area and volume of it.


class Shape
{
double a;
final double area(int A, int B, int C)
{
switch (A)
{
case 1: System.out.println(” Area of circle is”);
a=3.14*B*C;
System.out.println(a);
break;
case 2: System.out.println(” Area of Rectangle is”);
a=B*C;
System.out.println(a);
break;
case 3: System.out.println(” Area of Tringle is”);
a=(0.5)*B*C;
System.out.println(a);
break;
default : System.out.println(“Enter valid option “);
break;
}
return a;
}

}
class Circle extends Shape
{
int r,carea;
Circle(int B)
{
r=B;
}
double calc()
{
return area(1,r,1);
}
}
class Rectangle extends Shape
{
int length,width;
Rectangle(int B, int C)
{
length=B;
width=C;
}
double calc()
{
return area(2,length,width);
}
}

class Triangle extends Shape

{
int length,width;
Triangle(int B, int C)
{
length=B;
width=C;
}
double calc()
{
return area(32,length,width);
}
}
class HierarchicalDemo
{
public static void main(String args[])
{
Double carea, rarea,tarea;

Circle c=new Circle(5);
carea=c.calc();

Rectangle r=new Rectangle(10,10);
rarea=r.calc();

Triangle t=new Triangle(10,10);
tarea=t.calc();

}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s