Java Program To Calculate Area Of Circle |
5 Ways
in Java Programs 2 Comments
Java program to calculate or to print area of a circle in a simple method. If you
were new to java or at the beginning stage then, Check – 500+ simple Java programs for
beginners.
The following Java program to print the area of a circle has been written in five simple
different ways, static method, using constructor, Interface, inheritance with sample
outputs for each program.
Table of contents: 5 Simple ways to print AOC
1. Using Static Method
2. Using Interface
3. Inheritance
4. Using Constructor
5. Using Method
Also Check : Perimeter of Circle Java
# Below is the online execution tool, for the following program
Print Area Of Circle 5 Different Ways With
Examples
Below is the program to calculate the AOC in java, if you were new to java coding then
we can also share the complete step by steps with detailed explanation on how this java
program works, just below this code check it out.
1. Static Method
1 import java.util.Scanner;
2 class AreaOfCircle
3 {
4 public static void main(String args[])
5 {
6
7 Scanner s= new Scanner(System.in);
8
9 System.out.println("Enter the radius:");
10 double r= s.nextDouble();
11 double area=(22*r*r)/7 ;
12 System.out.println("Area of Circle is: " + area);
13 }
14 }
Output:
1 Enter the radius :
27
3 Area of circle : 154.0
As we know that formula in math is :
But, if you were written like that, then the system won’t understand, until and unless
you assign the value of “PIE” is 22/7. As usual , you always represent the values in
binary numbers.
The system can’t able to understand whether the given number is a number, or given
letter is a letter. All it understood as ” Character ” or just a “ Symbol “. Here goes the
complete step by step explanation of the above code and how it works.
Step – 1:
1 import java.util.Scanner;
( here ‘import’ is a keyword in java used to get features from inbuilt packages. here we
using a package called until it consists of many classes and we using one of the class
Scanner to get command over console which is the interface between user and
program. )
Step – 2:
1 public static void main(String args[])
( The main function, where the execution of program start from here onwards )
Step – 3:
1 Scanner s= new Scanner(System.in);
( 1. The scanner is a class used to scan the input data which was given by the user
through a console.
so to get access on a console we want to create an object Syntax:new Scanner(); after
creating an object that reference will store in variable ‘s’ )
Step – 4:
1 System.out.println("Enter the radius:");
( above code is giving instructions for the user to give the input for radius)
Step – 5:
1 double r= s.nextDouble();
( here above instruction is get input in the required format. first we want to know about
nextDouble() method it takes a token(collection of symbols divide by white space
example:”ABC”, “DEF” , “GHI”,”10″,”20″)
which is given by user.when user give 10 as input actually in user perspective it is
number but any number or string which entered on console by default those are
strings, 1o as string but we want 10 as number format for that we have method to
convert string to number(Int, Double, Float, Long…..) some of those method are:
1.nextDouble() ,
2 nextFloat(),
3.nextInt(),
4.nextLong()
5.nextShort()
6.next() or nextString() ).
Step – 6:
1 double area=(22*r*r)/7 ;
Step – 7: System.out.println(“Area of Circle is: ” + area); ( Once, you entered the
radius , the value stored in a particular function ( nextDouble(); ) and read those values
with the help of a scanner and display the output for a given value.
A : The major difference is where double can represent the output even after the
decimal point , whereas in ” Int ” only the numbers before decimal point will take into
consideration.
The second method for the above program #sample method – 2, with online compile
using command line arguments method :
Here is the sample line for an integer :
let arg[0]=”10″;
int r=Integer.parseInt(args[0]);//r=10;
The purpose of using the “Double ” is , whenever you enter the radius of a
particular number like “7” , the answer is 153.86 , numbers after decimal points will also
be displayed on the screen. Whereas if you use the “int” , the digits after the decimal
point will be loss.
If you have any doubts related to this program , do comment here. We are glad to help
you out.
2. Using Interface
There you go another program using the interface with sample outputs.
1 import java.util.*;
2 interface AreaCal
3 {
4 void circle();
5 }
6 class AreaOfCircle implements AreaCal
7 {
8 double area;
9 public void circle(double r)
10 {
11 area= (22*r*r)/7;
12 }
13 public static void main(String args[])
14 {
15 AreaOfCircle x;
16 Scanner s= new Scanner(System.in);
17 System.out.println("Enter the radius:");
18 double rad= s.nextDouble();
19 x=new AreaOfCircle();
20 x.circle(rad);
21 System.out.println("Area of Circle is: " + x.area);
22 }
23 }
3. Java Program Using Inheritance
Another program to print AOC using inheritance with sample example output.
1 import java.util.Scanner;
2 class AreaCalculation
3 {
4 double area;
5 void circle(double r)
6 {
7 area= (22*r*r)/7;
8 }
9 }
10 class AreaOfCircle extends AreaCalculation
11 {
12 public static void main(String args[])
13 {
14 Scanner s= new Scanner(System.in);
15 System.out.println("Enter the radius:");
16 double rad= s.nextDouble();
17 AreaOfCircle a=new AreaOfCircle();
18 a.circle(rad);
19 System.out.println("Area of Circle is: " + a.area);
20 }
21 }
Output:
1 Enter the radius:
2 63
3 Area of Circle is: 12474.0
4. Using Constructor
Using constructor to print AOC with outputs.
1 import java.util.Scanner;
2 class Area
3 {
4 double area;
5 Area(double r)
6 {
7 area= (22*r*r)/7;
8
9 }
10 }
11 class AreaOfCircle
12 {
13 public static void main(String args[])
14 {
15 Scanner s= new Scanner(System.in);
16 System.out.println("Enter the radius:");
17 double rad= s.nextDouble();
18 Area a=new Area(rad);
19 System.out.println("Area of Circle is: " + a.area);
20 }
21 }
Output:
1 Enter the radius:
2 15
3 Area of Circle is: 707.1428571428571
5. Using Method
1 import java.util.Scanner;
2 class AreaOfCircle
3 {
4 public static void main(String args[])
5 {
6 Scanner s= new Scanner(System.in);
7 System.out.println("Enter the radius:");
8 double rad= s.nextDouble();
9 double a=area(rad);
10 System.out.println("Area of Circle is: " + a);
11 }
12 static double area(double r)
13 {
14 return (22*r*r)/7;
15 }
16 }
More Programs:
Perimeter Of Circle
Perimeter of rectangle
Perimeter of square
Area Of Rectangle
Area Of Triangle