I am trying to write a program that calculates the surface gravity on each planet in our solar system. I have the printing of the results assigned to a static method. However, I have a problem. There are two for statements I am using in this program. The last for statement is causing me trouble. It calls back up the printResults static method to use in the main method. However, on the last variable, which is surfaceGravity, the program is not printing out all of the values of surfaceGravity. It is just printing out the last one. How should I get it to print out all of the surfaceGravity values? Can somebody help me with this?
/**
* This program determines the surface gravity (g) on each planet in our solar system.
*
* @author John D. Barry
* @date 02/17/09
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
public class GravityV1
{
public static void printResults(String[] names, double[] diameter, double[] mass, int x, double surfaceGravity)
{
System.out.printf("%7s%35.2f%35.4e%35.2f\n",names[x],diameter[x],mass[x],surfaceGravity);
}
//main method
public static void main (String [ ] args)
{
double surfaceGravity = 0.0;
double[] mass = { 3.30E23, 4.869E24, 5.972E24, 6.4219E23, 1.900E27, 5.68E26, 8.683E25, 1.0247E26, 1.27E22 };
double[] diameter = { 4880000, 12103000.6, 12756000.3, 6794000, 142984000, 120536000, 51118000, 49532000, 2274000 };
String[] names = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
for (int y = 0; y < 9; y++)
{
surfaceGravity = (((6.67E-11) * (mass[y])) / (Math.pow(diameter[y] / 2, 2)));
System.out.printf("%25.2f\n", surfaceGravity);
}
for (int x = 0; x < 9; x++)
{
printResults(names,diameter,mass,x,surfaceGravity);
}
}
}