Jump to content

Help with CircleStats code

- - - - -

  • Please log in to reply
2 replies to this topic

#1
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
Here is my code

import java.util.Scanner;

/**

 * This is the CircleStats program.

 * 

 * @author 

 * @version 

 */

public class CircleStats

{

    /**

     * Write a one-sentence summary of your main method here.

     */

    public static void main(String[] args) 

    {

        Scanner cin = new Scanner(System.in);

        

        System.out.print("Enter the radius of a circle: ");

        double radius = cin.nextDouble();

        

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

        System.out.print("Area :" + area);

        

        double circ =  2 * Math.PI * radius;

        System.out.print("Circumference: " + circ);

    } 

}



When I try to compile it highlights "double area = Math.PI * Math.sqrt(radius, 2);"
and says "java.lang.Math cannot be applied to (double,int)"

so I don't know how I'm suppose to fix that. help please

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Math.sqrt() can only be applied to double, not (double, int). Do this:
double area = Math.PI * Math.sqrt(radius);
But if I remember my math, the area of a circle is not pi * sqrt(radius), but rather pi * radius ^ 2, which is done like this:
double area = Math.PI * radius * radius;
You could use Math.pow(), but ehh.
Wow I changed my sig!

#3
heartybowl

heartybowl

    Newbie

  • Members
  • PipPip
  • 17 posts
you're right haha. it's pi r squared. i don't know where i got the sqrt from.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users