Reputation: 267
I am having trouble on fixing my program, In my last two lines of code, when I call the static method for surfaceGravity, the compiler complains about the argument radius with the statement "incompatible: double cannot be converted to a double[]" and when I call the static method for display, the compiler complains about the argument g with the statement "cannot find symbol". How do I fix this? Any help will be greatly appreciated. Below is my code:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class GravityV1{
// calculate surface gravity static method
public static double[] surfaceGravity(double G, double[] M, double[] r){
// surface gravity variable
double[] g = new double[M.length];
for(int i = 0; i < g.length; i++){
// equation for calculating surface gravity
g[i] = (G * M[i]) / Math.pow(r[i], 2);
}
// return statement
return g;
}
// displaying static method
// write results to text file static method
public static String[] writeToFile() throws IOException{
int index = 0;
String[] data = new String[index];
File filename = new File("/Users/timothylee/Gravity1.txt");
Scanner inFile = new Scanner(filename);
while(inFile.hasNext()){
data[index] = inFile.next( );
index++;
}
inFile.close();
return data;
}
public static void main(String args[]) throws IOException{
// declare array for months
String[] Planets = new String[8];
// initialize array
Planets [0] = "Mercury";
Planets [1] = "Venus";
Planets [2] = "Earth";
Planets [3] = "Mars";
Planets [4] = "Jupiter";
Planets [5] = "Saturn";
Planets [6] = "Uranus";
Planets [7] = "Neptune";
// declare array for mass
double[] mass = new double[8];
// initialize array
mass [0] = 3.30E23;
mass [1] = 4.869E24;
mass [2] = 5.97E24;
mass [3] = 6.4219E23;
mass [4] = 1.900E27;
mass [5] = 5.68E26;
mass [6] = 8.683E25;
mass [7] = 1.0247E26;
// declare array for diameter
double[] diameter = new double[8];
// initialize array
diameter [0] = 4880;
diameter [1] = 12103.6;
diameter [2] = 12756;
diameter [3] = 6794;
diameter [4] = 142984;
diameter [5] = 120536;
diameter [6] = 51118;
diameter [7] = 49532;
double[] surfaceG = new double[8];
// // convert to radius
// declare radius variable
double radius = 0;
// for each loop for displaying
for(double calc : diameter){
// calculation for converting to radius
radius = calc / 2;
}
// // calculate surface gravity
// declare and initialize universal gravity constant;
double G = 6.67384 * Math.pow(10, -11);
// call surface gravity method
surfaceGravity(G, mass, radius);
// display result
display(Planets, radius, mass, g);
}
}
Upvotes: 0
Views: 182
Reputation: 11
You are trying to convert a double
into a double[]
variable, which doesn't make sense.
double[] surfaceGravity(double G, double[] M, double[] r)
in prototype, 'r
' is an annary. However, radius
in 'surfaceGravity(G, mass, radius);
' is double
primitive.
Upvotes: 0
Reputation: 76
In this line:
public static double[] surfaceGravity(double G, double[] M, double[] r){
You declare that r should be of type double array, but in your main method here:
double radius = 0;
You declare radius to be of type double. Clearly these two are incompatible.
Also, in your main method when you call:
surfaceGravity(G, mass, radius);
Which returns a double array, you never assign the return value to anything. You can resolve this by saying:
double[] g = surfaceGravity(G, mass, radius);
Upvotes: 0
Reputation: 553
To fix your first problem you should replace your code:
// declare radius variable
double radius = 0;
// for each loop for displaying
for(double calc : diameter){
// calculation for converting to radius
radius = calc / 2;
}
with this:
// declare radius variable
double radius[] = new double[diameter.length];
// for each loop for displaying
for (int i = 0; i < radius.length; i++){
// calculation for converting to radius
radius[i] = diameter[i] / 2;
}
And to fix second problem you should replace:
// call surface gravity method
surfaceGravity(G, mass, radius);
with this:
// call surface gravity method
double[] g = surfaceGravity(G, mass, radius);
Upvotes: 1
Reputation: 15418
double[] surfaceGravity(double G, double[] M, double[] r)
you are calling this function with wrong type of parameter:
surfaceGravity(G, mass, radius);
radius
is primitive type variable with double
. The third parameter to be passed must be an array. So you must pass pass an array in place of radius. Change your method's parameter type:
double[] surfaceGravity(double G, double[] M, double r)
, the compiler complains about the argument g with the statement "cannot find symbol". How do I fix this?
you haven't declared any variable g
in your class context or main()
function context. So declare it with relevant type and in proper context.
Upvotes: 0
Reputation: 4046
as the compiler says your radius
variable is type of double
but your function requires array of doubles
Upvotes: 0