Global Positioning System Reference
In-Depth Information
With two MassObject s 3 coded with the laws of gravity and force, creating
two mass objects is pretty straightforward:
class MassObject { double mass, position; }
For programmers familiar with procedural programming, this MassObject
is like a record set storing the mass' attributes. The programming environ-
ment, where the objects are created for the actual simulation, is given by
the main(...) method. The two participating objects are created by
MassObject earth = new MassObject(5.98e24, 0 );
MassObject moon = new MassObject(7.35e22, 384404 );
Physics teaches that all masses are aware of each other|every mass
is interacting with every other mass in the universe continuously. In the
simulation, the mass objects, earth and moon, have to be introduced to
each other:
.interactWith(MassObject mass)
At this point the MassObject becomes more than a record set. Any
number of objects can be instantiated from one class, and their methods
are familiar with other objects created from the same class. This fact is
used in object-oriented programming to transfer or curry a two-argument
function distance(moon,earth) known from procedural languages into a
one-argument function, moon.distanceTo(earth) .
Basically the method interactWith introduces the other mass to interact
with and acts as the kickoff event to start the interaction:
earth.interactWith( moon );
moon.interactWith( earth );
As in the real world, where earth and moon are aware of each other,
the simulated objects are connected via object references. Both objects are
able to inquire the other's state (i.e., position) and use it to modify its own
state (i.e., current speed) by using distanceTo() . The simulation does not
give a clue where mass comes from, but the program environment serves
as the universe into which masses are created to interact. The MassObject
shows the world from each object's perspective. Following the physical
model, force is not a mass attribute and is therefore not represented as a
class member!
3 You should follow the code listings as you read.
 
Search WWH ::




Custom Search