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);
^^ ^
Answer from David Yaw on Stack Overflowjava - How to use Math.PI and Math.pow - Stack Overflow
[Java] Using PI question
Calculating of PI from sin() function in Java - Mathematics Stack Exchange
[Java] Is there any way to access true Pi in Java?
Videos
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);
}
}
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?
Sorry, but this is not a good idea. The formula that you saw essentially expresses that when
is small, and the smaller
the more exact the approximation. It is valid for angles in radians.
When the angles are in degrees, this relation becomes
where
denotes the sine of an angle in radians. So you hope to evaluate
If the function is not available, you will have to emulate it with an explicit conversion, using
so that
So, not only this does not allow you to compute as it requires preliminary knowlegde of
, but it will do that in a very inefficient and inaccurate way, actually replacing
by
. You will spend much energy to go round in circles.
Even when a function is available, this approach is wrong because the
will do the conversion from degrees to radians anyway (using a hard-coded value of
), and you will have to use an angle so small that
numerically, and there is no more point computing the sine.
A less "schizophrenic" approach is using
(in radians).
I am assuming your sin(x) function takes radians, as stated in the OP. Start by assigning x = 3 and then iterate the statement x = x + sin(x) a few times (three iterations should work well). When you are done, x will contain a good approximation to .
How does it work? We are using "fixed point iteration" to approximate a root of near
. Reference: Wikipedia, "Fixed-point iteration"
The Math.PI is equal to 3.141592653589793, which isn't really all of pi of course. Is there any way to access like more of it? I'm making a simple program that asks the user for a number (number of the precision of an output of pi) and it feels a little lackluster since it only goes to 15 decimal places.
Is there any way around it? Either way, I'd love to understand why it's limited how it is