0

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.
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102

2 Answers2

0

When you do 4/3 that is integer division so the result will always be 1 when you really want 1.33333... You should do

volume = 4.0/3.0 * Math.PI * Math.pow(radius, 3);

The 4.0/3.0 will ensure you get the correct result by dividing doubles instead of integers.

System.out.printf("The volume is %.5f.\n", volume);
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

4/3 will compute to 1 becuase of integer division. To actually get 1.333... you have to do

4f / 3

This will cast the 4 into a float so you get a float as a result which can actually hold 1.333.

As for your output:

System.out.println("The volume is " + volume + ".");
CodedStingray
  • 390
  • 1
  • 10