//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>
*/
//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>
*/
Very informative article.Thank you author for posting this kind of article .
ReplyDeletehttp://www.wikitechy.com/view-article/write-a-c-program-to-find-area-of-triangle-with-example-and-explanation
Both are really good,
Cheers,
Venkat
https://www.wikitechy.com/
ReplyDeletehttps://www.wikitechy.com/
ReplyDeleteThanks For sharing this information Java Program To Calculate Area and Perimeter of Rectangle .
ReplyDelete