I am doing an assignment for my first JAVA programming. So far it has been going well but I am stuck at the last part with using math functions.
double radius;
double diameter;
double volume;
System.out.print("Enter a diameter of a sphere: ");
diameter = keyboard.nextDouble();
radius = diameter / 2;
volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);
System.out.print("Volume of the sphere is " + volume + ".");
I am trying to use the formula for finding a volume of a sphere using diameter input variable. But I keep getting just the PI as an output.
This is what I am supposed to do.
- Add a line that prompts the user to enter the diameter of a sphere.
- Read in and store the number into a variable called diameter (you will need to declare any variables that you use).
- The diameter is twice as long as the radius, so calculate and store the radius in an appropriately named variable.
- The formula for the volume of a sphere is V = 4/3 * PI * radius^3 Convert the formula to Java and add a line which calculates and stores the value of volume in an appropriately named variable. Use Math.PI for PI and Math.pow to cube the radius.
- Print your results to the screen with an appropriate message.
What am I doing wrong ? Also how do I make it so the volume displays with E notation?