Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

Program of Constructor in Java

WAP to Calculate Area of Triangle Using Parmetrsied Constructor and Area Of Rectangle Using Copy Constructor?

class AreaExample
{
    int a,b;
    float area;
   AreaExample(int a,int b)
   {
      this.a=a;
      this.b=b;
   }
    AreaExample(AreaExample o)
   {
      this.a=o.a;
      this.b=o.b;
   }
 
   void areaTriangle()
    {
       area=(a*b)/2;
    }
    void areaRectangle()
    {
        area = (a*b);
    }
    public String toString()
    {
         return "Area is "+area;
    }
    public static void main(String args[])
    {
        AreaExample obj = new AreaExample(100,20);
        obj.areaTriangle();
        System.out.println(obj);
        AreaExample obj1 = new AreaExample(obj);
        obj1.areaRectangle();
        System.out.println(obj1);
    }

}

Post a Comment

0 Comments