I'm having two problems with my code if you guys could possibly help me. The first problem is that volume = 4/3 * Math.PI * Math.pow(radius, 3);
is not calculating correctly and should be 4.18879 when I enter 1.0 for radius. I'm confused because the others are calculating correctly. For the second problem my homework requires me to add a period after I print the answer. I tried System.out.printf("The volume is %.5f\n", volume + ".");
But it printed the . on the next line instead of on the same line. I have tried a lot of other ways but I can't figure it out. Sorry for the terrible formatting this is my first post.
import java.util.Scanner;
public class P2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Declare variables for geometric formulas
double radius;
double circumference;
double area;
double volume;
// Instantiate scanner
Scanner keyboard = new Scanner(System.in);
// Prompt and read radius from keyboard
System.out.print("Radius? ");
radius = keyboard.nextDouble();
// Calculate circumference, area, and volume
circumference = 2 * Math.PI * radius;
area = Math.PI * Math.pow(radius, 2);
volume = 4/3 * Math.PI * Math.pow(radius, 3);
// Print circumference, area, and volume to console
System.out.printf("The circumference is %.5f\n", circumference);
System.out.printf("The area is %.5f\n", area);
System.out.printf("The volume is %.5f\n", volume);
// Declare variables for converting mass to energy
double energy;
double mass;
double speedOfLight = 299792458.0;
// Prompt and read mass from keyboard
System.out.print("Mass? ");
mass = keyboard.nextDouble();
// Compute the energy using the formula
energy = mass * (Math.pow(speedOfLight, 2));
// Print energy to console
System.out.printf("The energy is %.1f\n joules.", energy);
// Close scanner
keyboard.close();
}
}
1. sample code:
- Radius? 1.0
- The circumference is 6.28319.
- The area is 3.14159.
- The volume is 4.18879.
- Mass? 1.0
- The energy is 89875517873681760.0 joules.