Reputation: 826
I'm writing a program to calculate surface gravity (http://en.wikipedia.org/wiki/Surface_gravity) of planets (yes, I know I can just Google it, but where's the fun in that?) however the numbers are not correct, nor are my printf methods working to contain the numbers in 2.2 format. What am I doing wrong here? Thanks!
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;
public class GravityV1
{
static String [] planetName = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
static double [] planetMass = {3.30e23, 4.87e24, 5.97e24, 6.42e23,1.90e27, 5.69e26, 8.66e25, 1.03e26, 1.31e22};
static double [] planetDiameter = {3032, 7521, 7926, 4222, 88846, 74898, 31763, 30778, 1466};
static double universalConstant = 6.674e-11;
public static double getSurfaceGravity(double mass, double diameter)
{
double g = (universalConstant * mass)/Math.pow((diameter / 2), 2);
return g;
}
public static void printHeader()
{
System.out.println(" Planetary Data ");
System.out.println(" Planet Diameter (km) Mass (kg) g (m/s^2) ");
System.out.println("------------------------------------------------------------");
}
public static void tab()
{
System.out.print("\t\t");
}
public static void main(String [] args)throws IOException
{
PrintWriter outFile = new PrintWriter (new File("planetGravity.txt"));
printHeader();
outFile.println(" Planetary Data ");
outFile.println(" Planet Diameter (km) Mass (kg) g (m/s^2) ");
outFile.println("------------------------------------------------------------");
for(int index=0;index<planetName.length;index++)
{
double gravity = getSurfaceGravity(planetMass[index], planetDiameter[index]);
System.out.print(planetName[index] + "\t\t");
System.out.printf("%2.2f", planetDiameter[index]);
tab();
System.out.printf("%2.2f", planetMass[index]);
tab();
System.out.printf("%2.2f", gravity);
System.out.println();
outFile.println(planetName[index] + "\t\t" + (planetDiameter[index]) + "\t\t" + planetMass[index] + "\t\t" + gravity);
}
outFile.close();
}
}
Upvotes: 0
Views: 161
Reputation: 306
You need to do some conversion of units in order to fix your calculations.
Your planet diameters are given in miles. You need to convert them into meters. You could do this with some multiplications in your planetDiameter array. For example, replace 3032 with 3032 * 1000 * 1.609344. Or you could do it in getSurfaceGravity
:
double g = (universalConstant * mass)/Math.pow((1000 * 1.609344 * diameter / 2), 2);
You should divide g by Earth's surface gravity (9.80665 m / s^2) before returning it from getSurfaceGravity
. So the last line becomes:
return g / 9.80665;
Dividing by Earth's surface gravity is required because you say you're expecting 0.38 for Mercury. That means you want values in units of Earth's gravity. (Mercury's surface gravity = 0.38 * [Earth's surface gravity]).
As has been suggested in the comments, use %2.2e to format your numbers in scientific notation.
Agreed that it is more fun to calculate it yourself than to just Google it!
Upvotes: 2