TechArena Community How to calculate area of Circle in Java?

#1
28-11-2009
 Member Join Date: May 2009 Posts: 1,187
How to calculate area of Circle in Java?

Hi,
I am second year computer engineer student. As per our syllabus we have java language. In our last exam in java paper one program is asked like How to calculate Area of Circle? I used various method but unable to write right program. That's why I decide to take help of you. If you know how to write this program then please give me.
#2
28-11-2009
 Member Join Date: Apr 2008 Posts: 1,948
Re: How to calculate area of Circle in Java?

Code:

import java.io.IOException;

public class CircleArea
{

public static void main(String[] args)
{

int r = 0;

try

{

}

catch(NumberFormatException ne)

{

System.exit(0);

}

catch(IOException ioe)

{

System.out.println("IO Error :" + ioe);

System.exit(0);

}
double a = Math.PI * r * r;

System.out.println("Area of  circle is " + a);

}
}
#3
28-11-2009
 Member Join Date: May 2008 Posts: 2,008
Re: How to calculate area of Circle in Java?

Hey I am not writing any program for you rather I give you logic behind it. You have to use following formula to calculate area of circle.

The formula is as follows:

Area of circle=Pi *r*r

You have to take radius from the user and save it in "r".
#4
28-11-2009
 Member Join Date: Apr 2008 Posts: 2,001
Re: How to calculate area of Circle in Java?

Code:
import java.io.*;
class AreaOfCircle
{
public static void main(String[] args){
try{
System.out.println("Enter Radius of Circle  : ");
System.out.println("Area of Circle : "+area1);

}
catch(Exception e){
System.out.println("Error : "+e);
}
}
}
#5
28-11-2009
 Member Join Date: May 2008 Posts: 2,293
Re: How to calculate area of Circle in Java?

This is one of the simplest and easiest progran.

Code:
class StaticCircle
{
public double r;
double  pi=3.14;
public void circle(double r)
{
this.r=r;

}
public double area()
{
double a1 = r*r;
double piV= a1*pi;
return piV;
}
public static void main(String[] args)
{
StaticCircle  sCircle  = new StaticCircle ();
sCircle.circle(10);
double t = sCircle.area();
System.out.println("The area is " + t);
}
}

 Tags: