gravity in java

https://w‮tual.ww‬turi.com
gravity in java

Gravity is a force that attracts objects towards the center of the earth or any other planet. In Java, you can use the java.lang.Math class to perform calculations involving gravity.

Here's an example of how you can use the Math class to calculate the force of gravity between two objects:

double mass1 = 10; // mass of first object in kilograms
double mass2 = 5;  // mass of second object in kilograms
double distance = 100; // distance between the objects in meters

double G = 6.674E-11; // gravitational constant

double force = (G * mass1 * mass2) / (distance * distance);

This code calculates the force of gravity between the two objects using the formula F = G * (m1 * m2) / r^2, where F is the force, G is the gravitational constant, m1 and m2 are the masses of the objects, and r is the distance between the objects.

Note that this is just a simple example, and in a real-world application you may need to consider additional factors such as the mass and size of the objects, the acceleration due to gravity at the location of the objects, and other physical laws.

Created Time:2017-11-01 12:05:03  Author:lautturi