I am in AP Computer Science, and we just learnt how to use our own static methods. This is my second program using them, but this time arrays are involved, and I am getting really weird output. I was hoping that someone could help me figure out why, I assume that I am not properly returning the result array.
The purpose of the program is to calculate the surface gravity of all of the planets in the solar system. The 2 things that I need help with are:
- I do not know how to format scientific notation using
printf()
so that is really messed up. - My gravity array contains
[D@7248989f
for every number and I can't figure out why.
public static double[] surfaceGravity (double[] r,double[] m) {
double[] g = new double[r.length];
for (int count = 0; count < r.length; count++) {
g[count] = (6.67 * m[count]) / (r[count] * r[count]);
}
return g;
}
public static void printIntro() {
System.out.printf("%8s%17s%12s%12s%n","Planet","Diameter (km)","Mass (kg)","g (m/s^2)");
System.out.println("-------------------------------------------------------------------");
}
public static void printData(String[] planet, double[] r, double[] m, double[] g) {
for (int count = 0; count < r.length; count++) {
System.out.printf("%9s%9.0f%17.6f%12.2f%n",planet[count],(r[count] * 2),m[count],g[count]);
}
}
public static void main (String[] args) {
//initialize arrays
String[] planetNames = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
//double[] mass = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};
double[] mass = {3.3E+23,4.87E+24,5.97E+24,6.42E+23,1.9E+27,5.69E+26,8.66E+25,1.03E+26};
double[] radius = {2440000,6051000,6378000,3397000,71492000,60268000,25559000,24754000};
double[] gravity = surfaceGravity(radius,mass);
printIntro();
printData(planetNames, radius, mass, gravity);
//I added the line below to test the contest of my gravity array
for (double grav: gravity) {
System.out.println(gravity);
}
}
Because I can't post images, I am not sure how to get the output on here to appear as it does on my computer. This is my full code, so that only thing that you have to do to see my output is to stick this all into a class and run it.
In closing: Yes I am a noob, but I hope that you guys can help me.