Java Acceleration of Gravity in Java

AI Thread Summary
The discussion focuses on calculating the acceleration of gravity using the formula (G * M) / (d^2), where G is the gravitational constant and M is the mass of the Earth. A user initially implemented the formula incorrectly, resulting in an incorrect output of 3.990454E14 instead of the expected 9.803495445209855. The error stemmed from the order of operations, as the user did not use parentheses correctly in the expression. After clarification, the correct implementation was provided as accelGravity = (G * M) / (distCenter * distCenter), which resolved the issue. Proper use of parentheses is crucial in programming to ensure the intended order of operations is followed.
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;
}
}
 
Technology 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?
 
obeying said:
Okay, so I set it up wrong all together?

We just need parentheses: [M]G * M / (distCenter * distCenter)[/M].
 
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: 409
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.
 
Back
Top