P. Mangs
P. Mangs

Reputation: 3

How to print a static method in java that takes an int as parameter and returns a double

I'm having difficulty trying to print the result of the static method calcArea, which takes the int radius as parameter to calculate the area of a circle and returns the area.

Here's the code below 👇 Any help would be appreciated.

public class CircleArea {

    public int radius;

     public static void main(String[] args) {

          Scanner input = new Scanner (System.in);
          System.out.print("Enter the radius of the circle: ");

         int radius = input.nextInt();

          System.out.print("The area of the circle is:" + calcArea()); <-- ERROR HERE
     }

      public static double calcArea(int radius){          

          double  area = Math.PI * Math.pow(radius, 2);
          return area;          

      }
}

Upvotes: 0

Views: 4694

Answers (3)

Dale
Dale

Reputation: 191

call method calcArea, you need give a parameter,Here are the correct example"

public static void main(String[] args) {
      Scanner input = new Scanner (System.in);
      System.out.print("Enter the radius of the circle: ");
      int radius = input.nextInt();
      System.out.print("The area of the circle is:" + calcArea(radius));
 }

Upvotes: 0

user5478656
user5478656

Reputation: 276

The function calcArea() takes the value of radius and then returns area. To do this, you need to pass an argument to calcArea(). So, your code should be like this:

    System.out.print("The area of the circle is:" + calcArea(radius));

The error you're getting clearly points out that you're missing an argument.

Upvotes: 0

Joe C
Joe C

Reputation: 15684

Your call to calcArea needs a parameter passed in. Probably calcArea(radius).

Upvotes: 2

Related Questions