Wednesday, 9 October 2013

Program to Calculate Area of Circle, Rectangle, Triangle using Constructor Overloading

//Program to Calculate Area of Circle, Rectangle, Triangle using Constructor Overloading
//Program by Ahlam Ansari

import java.util.*;

class Area
{
    private int radius,length, breadth,areaRectangle;
    private final float pi=3.14f, half=0.5f;
    private float base, height, areaCircle,areaTriangle;

    Area()
    {
        radius=0;
        length=0;
        breadth=0;
        base=0.0f;
        height=0.0f;
    }

    void input(int r)
    {
        radius=r;
    }

    void input(int l,int b)
    {
        length=l;
        breadth=b;
    }

    void input(float b,float h)
    {
        base=b;
        height=h;
    }
   
    void AreaCircle()
    {
        areaCircle=pi*radius*radius;
        System.out.println("Area of Circle is "+areaCircle+"cm.square" );
    }

    void AreaTriangle()
    {
        areaTriangle=half*base*height;
        System.out.println("Area of Triangle is "+areaTriangle+"cm.square"   );
    }

    void AreaRectangle()
    {
        areaRectangle=length*breadth;
        System.out.println("Area of Rectangle is "+areaRectangle+"cm.square"   );
    }
}

class MainFunOverloaded
{
    public static void main(String arg[])
    {
        //Input from user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Radius of Circle as integer");
        int r=sc.nextInt();
        System.out.println("Enter the Length and Breadth of Rectangle as integer");
        int len=sc.nextInt();
        int bre=sc.nextInt();
        System.out.println("Enter the Base and height of Triangle as float");
        float b=sc.nextFloat();       
        float h=sc.nextFloat();
       
        //Create Object
        Area circle =new Area();
        Area rectangle =new Area();
        Area triangle =new Area();
       
        //Function Call
        circle.input(r);
        rectangle.input(len,bre);
        triangle.input(b,h);
       
       
        circle.AreaCircle();
        rectangle.AreaRectangle();
        triangle.AreaTriangle();
    }
}

/*
OutPut

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac MainFunOverloaded.java

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java MainFunOverloaded
Enter the Radius of Circle as integer
2
Enter the Length and Breadth of Rectangle as integer
2
3
Enter the Base and height of Triangle as float
1.2
2.2
Area of Circle is 12.56cm.square
Area of Rectangle is 6cm.square
Area of Triangle is 1.32cm.square

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>

*/

4 comments: