Computer Science, asked by CopyThat, 10 hours ago

Write a program to calculate the area of a circle using static and non static variables.
[JAVA only]

Answers

Answered by Oreki
14

\textbf{Program}

   \texttt{import java.util.Scanner;}\\\\\texttt{public class CircleArea \{}\\\\\texttt{\hspace{1.5em} static double area;}\\\\\texttt{\hspace{1.5em} public static void main(String[] args) \{}\\\texttt{\hspace{3em} System.out.println("Radius - ");}\\\texttt{\hspace{3em} double radius = new Scanner(System.in).nextDouble();}\\\\\texttt{\hspace{3em} area = Math.PI * radius * radius;}\\\texttt{\hspace{3em} System.out.println("Area of a circle - " + area);}\\

   \texttt{\hspace{1.5em} \}}\\\texttt{\}}

 

\textbf{Sample I/O}

   \texttt{Radius - \textit{13}}\\\texttt{Area of a circle - \textit{530.929158456675}}

Answered by Anonymous
20

public class Circle

{

    static double PI = 3.145;

    int radius;

    public void setRadius(int r)

    {

          radius = r;

    }

    public double calculateArea()

    {

          double area = PI * (radius * radius);

          return area;

   }

 

   public static void main(String args[ ])

   {

         int radius = 4;

         double result;

         Circle myCircle = new Circle();

         myCircle.setRadius(Rad);

         result = myCircle.calculateArea();

         System.out.println(''Value of PI is : '' + Circle.PI);

         System.out.println(''Radius of circle is : '' myCircle.radius);

         System.out.prinltn(''Area of circle is : '' + result);

    }

}

The output will be :

Value of PI is : 3.1415

Radius of circle is : 4

Area of circle is : 50.264

Similar questions