Acceleration of Gravity in Java

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 18K views
obeying
Messages
8
Reaction score
0
Compute the acceleration of gravity for a given distance from the Earth's center, distCenter, assigning the result to accelGravity. The expression for the acceleration of gravity is: (G * M) / (d2), where G is the gravitational constant 6.673 x 10-11, M is the mass of the Earth 5.98 x 1024 (in kg) and d is the distance in meters from the Earth's center (stored in variable distCenter).

public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

***Student code here***

System.out.println("accelGravity: " + accelGravity);
return;
}
}----------------------------------------------------------------------------------------------------------------------------------------

The answer has to compute to 9.803495445209855 however, I'm getting the answer 3.990454E14

This is how I inputted the code, did I format it wrong?


public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

accelGravity = (G * M / distCenter * distCenter);

System.out.println("accelGravity: " + accelGravity);
return;
}
}
 
Physics news on Phys.org
obeying said:
Compute the acceleration of gravity for a given distance from the Earth's center, distCenter, assigning the result to accelGravity. The expression for the acceleration of gravity is: (G * M) / (d2), where G is the gravitational constant 6.673 x 10-11, M is the mass of the Earth 5.98 x 1024 (in kg) and d is the distance in meters from the Earth's center (stored in variable distCenter).

public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

***Student code here***

System.out.println("accelGravity: " + accelGravity);
return;
}
}----------------------------------------------------------------------------------------------------------------------------------------

The answer has to compute to 9.803495445209855 however, I'm getting the answer 3.990454E14

This is how I inputted the code, did I format it wrong?


public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

accelGravity = (G * M / distCenter * distCenter);

System.out.println("accelGravity: " + accelGravity);
return;
}
}

Hi obeying! Welcome to MHB! ;)

If we read [M](G * M / distCenter * distCenter)[/M] from left to right, as equal priority operations are supposed to be applied, we get [M]((G * M / distCenter) * distCenter)[/M], which is actually equal to [M](G * M)[/M]...
 
I like Serena said:
Hi obeying! Welcome to MHB! ;)

If we read [M](G * M / distCenter * distCenter)[/M] from left to right, as equal priority operations are supposed to be applied, we get [M]((G * M / distCenter) * distCenter)[/M], which is actually equal to [M](G * M)[/M]...

Okay, so I set it up wrong all together?
 
I like Serena said:
We just need parentheses: [M]G * M / (distCenter * distCenter)[/M].

It worked! Thank you so much, I can't believe I was that close to getting it.
 

Attachments

  • Screen Shot 2016-08-13 at 3.53.07 PM.png
    Screen Shot 2016-08-13 at 3.53.07 PM.png
    46.4 KB · Views: 445
obeying said:
Compute the acceleration of gravity for a given distance from the Earth's center, distCenter, assigning the result to accelGravity. The expression for the acceleration of gravity is: (G * M) / (d2), where G is the gravitational constant 6.673 x 10-11, M is the mass of the Earth 5.98 x 1024 (in kg) and d is the distance in meters from the Earth's center (stored in variable distCenter).

public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

***Student code here***

System.out.println("accelGravity: " + accelGravity);
return;
}
}----------------------------------------------------------------------------------------------------------------------------------------

The answer has to compute to 9.803495445209855 however, I'm getting the answer 3.990454E14

This is how I inputted the code, did I format it wrong?


public class GravityCalculation {
public static void main (String [] args) {
double G = 6.673e-11;
double M = 5.98e24;
double accelGravity = 0.0;
double distCenter = 0.0;

distCenter = 6.38e6;

accelGravity = (G * M / distCenter * distCenter);

System.out.println("accelGravity: " + accelGravity);
return;
}
}

accelGravity = ((G * M) / (distCenter * distCenter))
 
Does anyone know why the parenthesis matter this time ? When the program teaches that parentheis are not calculated but onnly used to keep things looking neat.
 
Khbv said:
Does anyone know why the parenthesis matter this time ? When the program teaches that parentheis are not calculated but onnly used to keep things looking neat.
Multiplication and division have equal priority and are evaluated from left to right.
It means that the parentheses around (distCenter * distCenter) are required.
All other parentheses are redundant.