import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
public class GravityV1
{
// Calculate surface gravity
public static double[] surfaceGrav( double []massKilo, double[] diameter)
{ double gravConstant = 6.67E-11;
double []surfaceGravi= new double[9];
for(int count = 0; count < surfaceGravi.length;count++)
{
surfaceGravi[count] = ((gravConstant * massKilo[count]) / (Math.pow(diameter[count]/2,2)));
}
return surfaceGravi;
}
//Print the results
public static void printResults(String [] planetName, double [] diameterKm, double [] massKilo, double[]gravity)
{
System.out.printf("%32s\n","Planetary Data");
System.out.printf("%-12s %-17s %7s %11s\n","Planet","Diameter(Km)","Mass(Kg)","g(m/s^2)");
System.out.println("--------------------------------------------------");
for(int counter = 0; counter < 9; counter++)
{
System.out.printf("%-14s %-14.0f %5.2e %11.2f\n",planetName[counter],diameterKm[counter],massKilo[counter],gravity[counter]);
}
}
//Output the results
public static void outputData(double[] gravity)throws IOException
{
PrintWriter outFile = new PrintWriter(new File("surfaceGravity.txt"));
double []outputGrav = gravity;
for(int index = 0; index < outputGrav.length;index++)
{
outFile.println(outputGrav[index]);
}
}
public static void main(String[]args) throws IOException
{
String [] planetName = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
double [] massKilo = {3.30E23,4.869E24,5.972E24,6.4219E23,1.900E27,5.68E26,8.683E25,1.0247E26,1.27E22};
double [] diameter = {4880000,12103600,12756300,6794000,142984000,120536000,51118000,49532000,2274000};
double [] diameterKm = {4880,12103.6,12756.3,6794,142984,120536,51118,49532,2274 };
double [] gravity = surfaceGrav(massKilo, diameter);
printResults(planetName, diameterKm, massKilo, gravity);
outputData(gravity);
}
}
}