JAVA Program to Find Area of Circle and Cylinder using interface

JAVA Program to Find Area of Circle and Cylinder using interface :


interface Shape
{
   final float pi=3.142f;
   public void display();
   public void area();
 }
 class Circle implements Shape
 {
     int r;
     float a;
      Circle(int r1)
       {
         r=r1;
        }
      public void area()
          {
           a=pi*r*r;
           }
      public void display()
         {
           System.out.println("area of circle : "+a);
         }
  }
  class Cylinder implements Shape
   {
     int r,h;
     float a;
      Cylinder(int r1,int h1)
       {
         h=h1;
         r=r1;
        }
      public void area()
          {
           a=2*pi*r*(h+r);
           }
      public void display()
         {
           System.out.println("area of Cylinder : "+a);
         }

  }
  class S
  {
     public static void main(String a[])
      {
        Shape s[]=new Shape[4];
          s[0]=new Circle(5);
          s[1]=new Cylinder(3,4);

          for(int i=0;i<s.length;i++)
           {
             s[i].area();
             s[i].display();
           }
       }
     }

OUTPUT:

area of circle : 78.55
area of Cylinder : 131.96399

Leave a Reply

Your email address will not be published. Required fields are marked *

12 − one =