Acceleration of Gravity in Java

Click For Summary
SUMMARY

The forum discussion centers on calculating the acceleration of gravity using Java, specifically the formula (G * M) / (d^2), where G is the gravitational constant (6.673 x 10-11), M is the Earth's mass (5.98 x 1024 kg), and d is the distance from the Earth's center. A user initially implemented the formula incorrectly, resulting in an incorrect output of 3.990454E14 instead of the expected 9.803495445209855. The solution involved adding parentheses to ensure proper order of operations, changing the expression to (G * M) / (distCenter * distCenter).

PREREQUISITES
  • Understanding of Java programming language
  • Knowledge of basic physics concepts, specifically gravitational force
  • Familiarity with mathematical operations and order of operations
  • Experience with debugging Java code
NEXT STEPS
  • Learn about Java operator precedence and how it affects calculations
  • Explore Java's Math library for advanced mathematical functions
  • Study gravitational physics to understand the implications of the formula
  • Practice debugging techniques in Java to resolve similar issues
USEFUL FOR

Java developers, physics students, and anyone interested in computational physics or programming calculations related to gravity.

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: 423
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
849
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
3
Views
3K