Acceleration of Gravity in Java

Click For Summary

Discussion Overview

The discussion revolves around the computation of the acceleration of gravity based on a given distance from the Earth's center, specifically focusing on the implementation in Java programming. Participants explore the correct formulation of the equation and the importance of parentheses in mathematical expressions within code.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant presents a Java code snippet intended to calculate the acceleration of gravity using the formula (G * M) / (d^2), but encounters an incorrect output.
  • Another participant suggests that the order of operations in the expression (G * M / distCenter * distCenter) leads to an unintended calculation, implying a misunderstanding of operator precedence.
  • It is proposed that adding parentheses to the expression, specifically using (G * M) / (distCenter * distCenter), resolves the issue and yields the correct result.
  • A participant questions the necessity of parentheses, noting that programming education often states they are not calculated but used for clarity.
  • Another participant clarifies that while multiplication and division have equal precedence, parentheses are necessary to ensure the correct order of operations in this specific case.

Areas of Agreement / Disagreement

Participants generally agree on the need for parentheses in the mathematical expression to achieve the correct calculation, but there is some uncertainty regarding the broader implications of parentheses in programming and their perceived role in clarity versus calculation.

Contextual Notes

There is a discussion about operator precedence in Java, particularly how multiplication and division are evaluated from left to right, which affects the outcome of expressions without parentheses.

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: 429
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
1K
Replies
8
Views
3K
  • · 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