Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 20th, 2013, 01:09 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 195
Time spent in forums: 1 Day 5 h 56 m 39 sec
Reputation Power: 1
Problem with Cubic Miles in Sun and Earth Java Program

Hello,

I was wondering if my use of the volume formular to determine the volume of the earth and sun, were used correctly to determine cubic miles, or is my answer only in square miles?

Here's my code:
Code:
```/* 4. The diameter of the sun is approximately 865,000 miles.
* The diameter of Earth is approximately 7600 miles.
* Use the methods in the class Math to calculate the following:
* The volume of Earth in cubic miles
* The volume of the sun in cubic miles
* The ratio of the volume of the sun to the volume of Earth
*/

import static java.lang.Math.*;

// This will import all available methods from the Math class into this program

public class Chapter2Exercise4 {

public static void main(String[] args){

final int sun_diameter = 865000;

final int sun_radius = sun_diameter / 2;

final int earth_diameter = 7600;

final int earth_radius = earth_diameter / 2;

double volume_of_earth = (4.0 / 3) * Math.PI * Math.pow(earth_radius, 2);

double volume_of_sun = (4.0 / 3) * Math.PI * Math.pow(sun_radius, 2);

int sun_to_earth_ratio = (int)(volume_of_sun / volume_of_earth);

System.out.println("Here is the diametere of the earth in miles: " + earth_diameter);
System.out.println("Here is the radius of the earth in miles: " + earth_radius);
System.out.println("Here is the volume of the earth in cubic miles: " + volume_of_earth);

System.out.println("Here is the diameter of the sun in miles: " + sun_diameter);
System.out.println("Here is the radius of the sun in miles: " + earth_radius);
System.out.println("Here is the volume of the sun in cubic miles: " + volume_of_sun);

System.out.println("Here is the sun to earth ratio: " + sun_to_earth_ratio);

}

}```

#2
September 20th, 2013, 06:41 PM
 MrFujin
Lord of the Dance

Join Date: Oct 2003
Posts: 3,355
Time spent in forums: 2 Months 2 Weeks 4 Days 20 h 56 m 38 sec
Reputation Power: 1828
You did some mistakes:
1. You use integer to store divisions and thereby losing all the decimals. rounding too early in the calculation will give the wrong answer.
Use double instead. (or float, can't remember which of them is best practise)

2. You got the function wrong for the volume. you should use r^3 instead of r^2.

Some information in case you don't know: Earth is not that good an example of a sphere, as its radius is smaller when "going" from south to north.

Last edited by MrFujin : September 20th, 2013 at 06:48 PM.

#3
September 21st, 2013, 12:17 PM
 HauntJemimah
Contributing User

Join Date: Aug 2013
Posts: 195
Time spent in forums: 1 Day 5 h 56 m 39 sec
Reputation Power: 1
Quote:
 Originally Posted by MrFujin You did some mistakes: 1. You use integer to store divisions and thereby losing all the decimals. rounding too early in the calculation will give the wrong answer. Use double instead. (or float, can't remember which of them is best practise) 2. You got the function wrong for the volume. you should use r^3 instead of r^2. Some information in case you don't know: Earth is not that good an example of a sphere, as its radius is smaller when "going" from south to north.

Oh okay. Thanks a bunch! I should have known to clearly use double throughout my variables. Good point.

I'm only doing it because it was an example in the Ivor Horton Beginning Java book (lol I know about Earth not being a nicely rounded sphere heh)

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Problem with Cubic Miles in Sun and Earth Java Program