Java program to calculate area of circle using method

Let’s learn java program to calculate area of circle using method.

Java program to calculate area of circle using method

java program to calculate area of circle using method

To find area of a circle using method in java, first we have to write calculateArea() method which accepts radius as parameter to calculate area of circle in java.

Also read – string toUpperCase() method in java

Then in the next step, in main() method we have to call calculateArea() method. Here’s the java program to find area of circle using method,

import java.util.Scanner;
public class AreaOfCircleMethod 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);  
      System.out.println("Please enter radius : ");
      double radius = sc.nextDouble();
      // area of circle in java
      double area = calculateArea(radius);  
      System.out.println("Area of circle : " + area);
      sc.close();
   }
   static double calculateArea(double r)
   {
      return (22 * r * r) / 7;
   }
}



Output:

Please enter radius : 5
Area of circle : 78.57142857142857

Similarly we can calculate area of circle without using method. Here’s the java program – click here.

Java program to calculate area and circumference of circle using methods

Here’s the java program,

import java.util.Scanner;

public class CalculateAreaOfCircleMethod 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);  
      System.out.println("Please enter radius to calculate area and circumference of circle: ");
      double radius = sc.nextDouble();
      double area = calculateArea(radius);  
      System.out.println("Area of circle: " + area);
      double circumference = calculateCircumference(radius);
      System.out.println("Circumference of circle: " + circumference);
      sc.close();
   }
   static double calculateArea(double r)
   {
      return (22 * r * r) / 7;
   }
   static double calculateCircumference(double rad)
   {
      return Math.PI * 2 * rad;
   }
}


Output:

Please enter radius to calculate area and circumference of circle: 10
Area of circle: 314.2857142857143
Circumference of circle: 62.83185307179586

Java program to find area of circle and rectangle

Here’s the java program to calculate area of circle and rectangle,

public class FindAreaOfCircleRectangle 
{
   public static void main(String[] args) 
   {
      int length = 30;
      int breath = 10;
      double radius = 5;
      System.out.println("Area of rectange is: " + length * breath);
      System.out.println("Area of circle is: " + (22 * radius * radius) / 7);
   }
}


Output:

Area of rectange is: 300
Area of circle is: 78.57142857142857

Java program to calculate area of circle rectangle and triangle using switch statement

Here’s the java program,

import java.util.Scanner;

public class FindAreaUsingSwitchStatement 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("MENU:");
      System.out.println("1.Area of circle");
      System.out.println("2.Area of triangle");
      System.out.println("3.Area of rectangle");
      System.out.println("Please enter any of the above option: ");
      int num = sc.nextInt();
      switch(num)
      {
         case 1: System.out.println("Please enter radius of circle: ");
         double radius = sc.nextFloat();
         double areaCircle = (22 * radius * radius) / 7;
         System.out.println("Area of circle is: " + areaCircle);
         break;
 
         case 2: System.out.println("Please enter base and height of triangle: ");
         double base = sc.nextFloat();
         double height = sc.nextFloat();
         double areaTriangle = (base* height) / 2;
         System.out.println("Area of triangle is: " + areaTriangle);
break;

         case 3: System.out.println("Please enter length and breadth of rectangle: ");
         int length = sc.nextInt();
         int breadth = sc.nextInt();
         int areaRectangle = length * breadth;
         System.out.println("Area of ractangle is: " + areaRectangle);
         break;

         default:System.exit(0);
      }
      sc.close();
   }
}


Output:

MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 1
Please enter radius of circle: 5
Area of circle is: 78.57142857142857

MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 2
Please enter base and height of triangle:
20
100
Area of triangle is: 1000.0

MENU:
1.Area of circle
2.Area of triangle
3.Area of rectangle
Please enter any of the above option: 3
Please enter length and breadth of rectangle:
50
20
Area of ractangle is: 1000

Java program to find area of circle and rectangle using interface

interface FindArea
{    
   double pi = 3.14159265359;    
   double calculate(double a, double b);    
}

class Rectangle implements FindArea    
{    
   public double calculate(double a, double b)    
   {    
      return(a * b);    
   }
}

class Circle implements FindArea    
{    
   public double calculate(double a, double b)    
   {    
      return(pi * a * a);    
   }    
}

public class FindAreaUsingInterface 
{
   public static void main(String[] args) 
   {
      Rectangle rect = new Rectangle();    
      Circle cir = new Circle();    
      FindArea area;
      area = rect;    
      System.out.println("Area of rectangle is: " + area.calculate(50, 30));      
      area = cir;    
      System.out.println("Area of circle is: " + area.calculate(10, 10));
   }
}


Output:

Area of rectangle is: 1500.0
Area of circle is: 314.15926535899996