Java Program To Calculate Area Of Circle :
in this tutorial we are learning how to Calculate Area of Circle in java using Different Methods programming Language.
Table of contents:
- Using Static Method
- Using Interface
- Inheritance
- Using Constructor
- Using Method
in this below program we Calculate the Area of Circle in Java.
Static Method :-
class AreaOfCircle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double r= s.nextDouble();
double area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);
}
}
Using Interface :-
import java.util.*;
interface AreaCal
{
void circle();
}
class AreaOfCircle implements AreaCal
{
double area;
public void circle(double r)
{
area= (22*r*r)/7;
}
public static void main(String args[])
{
AreaOfCircle x;
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double rad= s.nextDouble();
x=new AreaOfCircle();
x.circle(rad);
System.out.println("Area of Circle is: " + x.area);
}
}
Java Program Using Inheritance :-
class AreaCalculation
{
double area;
void circle(double r)
{
area= (22*r*r)/7;
}
}
class AreaOfCircle extends AreaCalculation
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double rad= s.nextDouble();
AreaOfCircle a=new AreaOfCircle();
a.circle(rad);
System.out.println("Area of Circle is: " + a.area);
}
}
Using Constructor :-
import java.util.Scanner;
class Area
{
double area;
Area(double r)
{
area= (22*r*r)/7;
}
}
class AreaOfCircle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double rad= s.nextDouble();
Area a=new Area(rad);
System.out.println("Area of Circle is: " + a.area);
}
}
Using Method :-
import java.util.Scanner;
class AreaOfCircle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double rad= s.nextDouble();
double a=area(rad);
System.out.println("Area of Circle is: " + a);
}
static double area(double r)
{
return (22*r*r)/7;
}
}