2
import java.util.Scanner;

public class Test {
    
    public static void main(String[] args) {
        
        Scanner userInput = new Scanner(System.in);
        
        final double PI = Math.PI;
        final double sphereVolumeConstant = (4/3);
        
        System.out.println("Volume of a sphere.");
        
        System.out.print("Radius of sphere: ");
        double radius = userInput.nextDouble();
        
        double volumeOfSphere = sphereVolumeConstant * PI * Math.pow(radius, 3);
        
        System.out.println("Volume = " + sphereVolumeConstant);
    }

}

Terminal:

Volume of a sphere.
Radius of sphere: 5
Volume = 1.0

I am trying to calculate the constant itself which keeps coming out to be an integer even though it shouldn't, or at least I don't think it should. Can someone please help/explain?

iknow
  • 4,796
  • 9
  • 19
  • 37
Rigel
  • 21
  • 1

3 Answers3

2

sphereVolumeConstant will always be 1.0. writing double before variable doesn't make every operation on it double. If You want to make sphereVolumeConstant to be equal to 1.(3) You have to write:

final double sphereVolumeConstant = (4.0/3);

Or:

final double sphereVolumeConstant = ((double) 4 / 3);

Now the operation will be made in double because 4.0 or (double) 4 is double.

iknow
  • 4,796
  • 9
  • 19
  • 37
1

Because the value of sphereVolumeConstant is assigned as (4/3), its value will always be 1.0 because of how integer division works. What you need to do is cast at least the 4 or the 3 to a double/float:

  1. final double sphereVolumeConstant = ((double)4/3);
  2. final double sphereVolumeConstant = (4/(double)3);
  3. final double sphereVolumeConstant = (4.0/3);
  4. final double sphereVolumeConstant = (4/3.0);

This thread explains why integer division behaves in this way.

parthlr
  • 316
  • 1
  • 13
0

Default is int for your dividend & divisor

In your example ( 4 / 3 ), the 4 and the 3 (your dividend & divisor) both default to being integers. So you are dividing one integer by another. You get a resulting integer as your quotient.

System.out.println( 4 / 3 ) ;

1

Then you cast that resulting integer to be a double, 1.0.

double a = ( 4 / 3 ) 

1.0

If you mean 4 or 3 to be floating-point numbers, say so. Mark them with a d for double or f for float.

( 4d / 3d ) 

1.3333333333333333

Or use decimal separator.

( 4.0 / 3.0 )

1.3333333333333333

BigDecimal

If you care about accuracy rather than speed-of-execution, use BigDecimal. Never use float/Float or double/Double for money or other matters demanding accuracy.

Pass a MathContext containing precision (total number of digits) and RoundingMode.

MathContext mc = new MathContext( 7 , RoundingMode.HALF_EVEN ) ;  // Banker's rounding.
BigDecimal d = new BigDecimal( "4" ).divide( new BigDecimal( "3" ) , mc ) ;

1.333333

Alternatively, pass scale (number of digits to the right of the decimal separator) and RoundingMode.

BigDecimal e = new BigDecimal( "4" ).divide( new BigDecimal( "3" ) , 2 , RoundingMode.HALF_EVEN ) ;

1.33

Example code

See all that code run live at IdeOne.com.

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915