Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info 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