Chapter 2 Excercise- Q4 Confusion
I m getting two different output in the programs which are almost same.
just the difference is '.0'. In the first program I have not added 'x.0' after the digits(where x is the integral part). and this thing is giving me 2 different answers.
Please explain why am I getting answers like that???
Code for the first program
1.
public class Exercise4 {
public static void main(String[] args){
double diameterSun = 865000;
double diameterEarth= 7600;
double volumeSun = 0;
double volumeEarth=0;
double radiusSun= diameterSun/2;
double radiusEarth = diameterEarth/2;
double fourOverThree = (4/3);
volumeSun =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusSun,3)));
volumeEarth =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusEarth,3)) );
double ratioVolume = (volumeSun/volumeEarth);
System.out.println("Volume of Sun = "+volumeSun+"\nVolume of Earth = "+volumeEarth+
"\n Ratio = "+ratioVolume);
}
}
the code for the 2nd version.
2.
public class Exercise4 {
public static void main(String[] args){
double diameterSun = 865000.0;
double diameterEarth= 7600.0;
double volumeSun = 0.0;
double volumeEarth=0.0;
double radiusSun= diameterSun/2.0;
double radiusEarth = diameterEarth/2.0;
double fourOverThree = (4.0/3.0);
volumeSun =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusSun,3)));
volumeEarth =(double) (fourOverThree*(Math.PI)*(Math.pow(radiusEarth,3)) );
double ratioVolume = (volumeSun/volumeEarth);
System.out.println("Volume of Sun = "+volumeSun+"\nVolume of Earth = "+volumeEarth+
"\n Ratio = "+ratioVolume);
}
}
Last edited by devanshj95; April 15th, 2013 at 08:00 AM..
|