This
import java.lang.Math.*;
imports all (accessible) types declared within Math.
This
import java.lang.Math;
is redundant because Math is part of java.lang which is imported by default.
Both will require that you use
Math.PI
to access the field.
This
import static java.lang.Math.PI;
imports the static member Math.PI so that you can use its simple name in your source code.
This
import java.lang.Math.*;
imports all (accessible) types declared within Math.
This
import java.lang.Math;
is redundant because Math is part of java.lang which is imported by default.
Both will require that you use
Math.PI
to access the field.
This
import static java.lang.Math.PI;
imports the static member Math.PI so that you can use its simple name in your source code.
'Allow Math.PI as a reference to the PI constant' means that your code will have to look like this in order to work:
static double getCircumference(double radius ) {
return Math.PI * 2 * radius;
}
public static double getArea(double radius) {
return Math.PI * radius * radius;
}
What import java.lang.Math; does is importing the class java.lang.Math so you can reference it with Math instead of the qualified version java.lang.Math. import java.lang.Math.*; does the same for Math and all nested classes, but not it's members.
[Java] Using PI question
how to use math.pi in java - Stack Overflow
How do I use Math.PI in Java? - TestMu AI Community
[Java] The import java.util.Math cannot be resolved
Videos
So I know I can do something like a direct import (not sure if that's the real name) but without importing java.lang.Math I can do a Math.PI and use this value for pi. I have an program where I use it more than once, so I think I should make a constant out of it. Personally I don't know why I can't put a Math.PI wherever I need it, because it seems clear and readable already, but I think for my assignment I need to adhere to this convention of use a number more than once with meaning > make constant.
How can I do that though? I have an import java.lang.Math; at the top of my program. Then in my class (but before my main) I have a public static final double PI = Math.PI;..so was there no need to import the Math package? How should I best do this?
You're missing the multiplication operator. Also, you want to do 4/3 in floating point, not integer math.
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
^^ ^
Here is usage of Math.PI to find circumference of circle and Area
First we take Radius as a string in Message Box and convert it into integer
public class circle {
public static void main(String[] args) {
// TODO code application logic here
String rad;
float radius,area,circum;
rad = JOptionPane.showInputDialog("Enter the Radius of circle:");
radius = Integer.parseInt(rad);
area = (float) (Math.PI*radius*radius);
circum = (float) (2*Math.PI*radius);
JOptionPane.showMessageDialog(null, "Area: " + area,"AREA",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "circumference: " + circum, "Circumfernce",JOptionPane.INFORMATION_MESSAGE);
}
}