This article used to be an entire chapter of the book until it was decided to focus on the technical implementation before and to spare the reader physical formulas. Anyhow the article describes the initial ideas leading to the ROAF and readers interested in physical models can follow the idea of physical objects. After implementing scientific laws in Software Objects they can be placed in a simulation environment.
You will find the two classes roaf.book.intro.MassObject and ProceduralGravityModel to accompany this article in the roaf book resources archive.
You can’t find literature about object orientation without a reference to the real world.
Java White Paper > 3. Java is Object Oriented > 3.2 What Are Objects?
”…object technology is a collection of analysis, design, and programming methologies that focuses design on modeling characteristics and behavior of objects in the real world …objects are software programming models. In your everyday life, youre surrounded by objects: cars, coffee machines, ducks, trees…
An objects behavior is defined by its methods…you can build entire networks and webs of objects that pass messages between them to change state. This programming technique is one of the best ways to create models and simulations of complex real-world systems.”
Modeling of real world objects goes way beyond controlling electrical devices. The object oriented approach made programming languages much more semantical. Computer language, mathematical and human language have moved closer together.
This chapter will look at physical objects and analyze how their behavior can described. It will use
some formulas and comes with a java application. Don’t bother with formulas or source code too
much, they are only supposed to support the points to be made.
What does modeling of real world objects actually mean? What is real?
The question about reality was certainly not introduced with computers. Hundreds of years ago the first scientists started to question our environment – with systematic experiments.
Just like the scientist, the object oriented programmer is using techniques to abstract details to understand the behavior of objects, things. If behavior is reproducible this indicates a physical law to be described in mathematical formulas. Physics describe ’real world behavior’ in the language of mathematics. Formulas describe how objects behave. Newton’s laws of motion describe how masses attract each other.
With these analogies between real world objects and software objects, simulation and experiment, mathematical formulas and object oriented programming make it worthwhile to take a closer look at models in physics. Why not transfer the entire scientific knowledge into software objects?
Experiments have established the science of physics and simulations have always driven the development of fast computers. It was only a matter of time (performance) until computers had replaced complicated experiments with simulations. Todays simulations save expensive prototyping of complicated mechanisms and machines. Massive computer farms are being used to predict the wheather and allow to peek into the future.
If the idea of software objects is derived from real world objects, wouldn’t it be
the ultimate test of object oriented software to build a real world simulator?
Can software objects be programmed to behave like physical objects?
Sounds like a nice blueprint to create some objects and fill them with mathematical formulas found in any schoolbook. The testing could be done by comparing simulations with the real world and in the end: How can you tell the difference between the virtual and the real world? Simply by finding the difference!
Mother nature as the project manager should help to minimize discussions!
How can a mathematical formula describe the real world?
In order to answer this some fundamental qualities of physical laws have to be introduced:
Mass is any ’thing’ that has a dimension, a measurable size and a weight.
Motion is the change of position in space & time.
How is mass, space and time quantified?
To keep the math simple physicists have agreed on unit systems. This book will stick to the MKS system, which simply defines that distances are measured in meter [m], masses in kilogram [k] and time in seconds [s].
MKS suggests a rough magnitude of the (classical) physical phenomenons and basically matches the environment a human being can perceive. Most people are between one and two meters tall, while they can see down to a resolution of a millimeter and are able to move (run, hike) over many kilometers. People can weigh up to some hundred kilograms and the human hand can feel a body in the magnitude of one gram. People are able to estimate the duration of one second, which is roughly the time of one heartbeat. The good news is that the actual laws of physics are not dependent on these units and using miles instead of kilometers doesn’t change the law. You could say the MKS is anthropocentric.
Galileo Galilei is often referred to as the inventor of the systematic experiment, of science! By experimenting with falling bodies he was able to eliminate the impression that ’heavy’ bodies fall faster than the lighter ones. He strived to understand motion of bodies and describe it in mathematical formulas. Intuitively he was looking for equations to describe the first laws of motion.
1638 Galileo defined the concept of inertia, fundamental to mechanics:
Inertia is the quantity that solely depends upon mass.
Inertia is the resistance an object has to a change in its state of motion.
Isaac Newton picked up the idea of mass and inertia and realized that the force attracting the apple from the tree is the same that holds the planets on their trajectory. With his model of universal gravity he distinguished mass from weight. Newton’s idea of force was a generalization of Galileo’s idea. Galilei and Newton gave mass a physical quality and a quantitative model in form of mathematical equations.
Here’s the law derived from Galilei’s idea of inertia leading to force, mass and motion:
The force is defined via the acceleration of mass.
A car’s acceleration is determined by the horsepower (force over time) moving its mass.
Acceleration is always proportional to the applied force on a given mass.
In order to move a body force has to be applied over a time slice Δt = t1 - t0. etc.
Newton’s law of gravity is described by the formula:
The left side is the actual law stating that the force of gravity is proportional to product of the
involved masses and the distance between them. The right side simply introduces a ’gravity constant’
to get the dimensions straight and form an equation.
The formula does not explain what gravity is and why masses attract each other,
nor can anyone explain why masses are multiplied or why the distance is multiplied with itself.
Nevertheless the law can predict how they behave and makes the law applicable in simulations.
Newton got the idea of a gravitational force by watching the sky – and the apple. The moon surrounds the earth, which indicates that it is constantly forced to move on a curved line.
A simple java simulation will demonstrate the behavior of two masses.
The purpose is to experience the physical law of gravity attracting the two masses earth and moon under laboratory conditions. After two masses have been created at two points (coordinates) with a given distance at a time t0 they are immediately attracted to each other by gravitational force. The intention of the experiment is to determine the duration Δt until the two masses touch at a time tx.
With the values mmoon and mearth and the distance r it seams trivial to calculate the gravitational force with Newton’s law (2) and then calculate the acceleration with Galileo’s law (1). But it turns out that its a little more tricky to deal with reality. The problem is that the force of gravity accelerates and moves the masses, which changes the distance between them, which again changes the force.
To avoid higher mathematics the simulation will use formula (2) at the time t0 to get the force and then use (1) to get the resulting acceleration. Then the trick is to define timeslicesΔt, during which the force and acceleration are assumed to be constant. After t0 + Δt at the time t1 the masses speeds and distance are adjusted and the next time slice is processed until the distance is close to zero.
Before moving to the next section you can prepare your environment by installing java and opening the file roaf.book.intro.MassObject.java with your editor. As you read you might want to experiment with the MassObject and experience the effects of different time slices. Don’t worry about the details of the code and simply focus on modifying the experiment.
Creating two mass objects is pretty straight forward:
For programmers familiar with procedural programming this MassObject is like a record set storing the masses’ attributes. The programming environment, where the objects are created for the actual simulation, is given the main(String args) method. The two participating objects are created by
For a one dimensional simulation of two masses on a straight line the positions define the coordinate system and the distance between them. For plausible reasons the place of the collision must be somewhere between 0 and 384404. Probably closer to 0, because the earth has more mass (and inertia) than the moon.
What about the interaction?
Physics teach that all masses are aware of each other. Every mass is interacting with every other mass in the universe continuously. In the simulation the MassObjects earth and moon have to be introduced to each other, which is done with the method
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 naturally 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.
Right after earth is introduced to moon and vice versa, the interactions begin almost at the same time. The end of the main method has been reached, but the software objects are still ’running’. In this case each object runs its own Thread, which is another ’program’ embedded in the method .run(). This is similar to turning on a TV via remote control, the remote control runs out of battery power, but the TV show keeps running nevertheless.
An object can be more than a record set, it can be a program.
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 others ’state’ (i.e. position) and use it to modify it’s own state (i.e. current speed) by using distanceTo(). The simulation does not give a clue where mass comes from, but the program environment (i.e. main(..)) serves as the universe into which masses are created to interact.
You can experiment with the simulation and test where two equal masses would meet.
Where would the two masses collide, if one had the double mass of the other?
So what’s the difference between the procedural and object oriented simulation?
The MassObject shows the world from each object’s perspective. Following the physically model
force is not a mass attribute and is therefore not represented as a class member! As soon as the earth
object is ’informed’ about the moon’s existence it looks out of the window to determine the other
mass, the distance to it and then the resulting gravity force between them, which is actually moving
The interaction between the masses is managed by the objects themselves!
Compared to the roaf.book.intro.ProceduralGravityModel as an example of the procedural approach, the simulation with two MassObject is much more intuitive. It is a model of the real world: gravity force moves each mass like a car is moved by the force of its engine, no strings attached.
The little simulation is also suited to teach a lesson about the law of actio and reactio, which states that each mass is applying the same amount of force to the other. This can be verified by commenting one of the lines in the main program. Each line (’earth interact with moon’ or ’moon interact with earth’) will find the time to collision without the other! It’s only a matter of coordinates – of relativity. Each object carries it’s own little world model!
Newton’s and Galileo’s laws were introduced as simple arithmetic formulas and modern computers can execute them in almost no time. The world model could get more complex by adding a third mass and a three dimensional space. At some point the complexity of the simulation could keep the processor so busy, that it would fall behind the ’real time’ of the represented real world experiment. The simple arithmetic could be improved with higher mathematics…
Physically speaking the gravity experiment takes place in a closed system. The beginning of the experiment at t0 is perfectly defined and nothing will influence the two masses from ’outside’. A mathematician can not accept rough estimated values for point xc and time tc of the collision. He would derive functions for given physical laws and calculate these values precisely. For the performance of the simulation this would be a relieve for the processor. At t0 the object could calculate tc and wouldn’t bother the processor until the event of the collision takes place.
Instead of going into mathematical formulas (and scaring away some readers) the following might give an understanding how mathematical ’qualities’ can simplify the way to solve physical problems. The gravity example was useful to demonstrate how science is searching for functions. Physicists are very often looking for the function of force in space F(r) in many different experiments.
Newton has generalized the definition of force by introducing the quality momentum as the product of mass and speed p = m⋅v. Momentum is useful for scenarios with changing mass Δm, like a rocket getting lighter by burning fuel (= mass). (Some centuries later the concept of momentum still holds for Einstein’s theory of relativity.)
By analyzing closed systems laws of conservation can be identified to predict results of the interactions. The earth-moon simulation can also be solved with the assumption that the total momentum of the system is conserved.
One of the most powerful concepts is the idea of energy. Energy is the money in physics. Like money can be traded to many things, energy is a theoretical value calculated in one scenario to be ’used’ in another one. Energy is never created, only transformed (and there is no such thing as inflation!). Nuclear power is transformed from heat into mechanical motion to ’produce’ electricity. The total energy of a closed system is always conserved and makes it very practical as equations (= equal) can describe the transformations inside the system.
In the earth-moon scenario the potential energy of gravity is transformed into kinetic energy of each mass body. By increasing the speed the body is ’filled’ with energy and this energy can potentially be retransformed into another form of energy.
Although energy is a mathematical construct, it’s an intuitive term of our language.
How can a simulation take advantage of mathematics?
The gravity example was burning CPU power during the entire time of the simulation. A more scientific approach would be to take all factors into account at t0 and then look for some ’world formula’ to describe the experiment. It would be very useful to have functions for the object’s states position and velocity at any given time: x(t) and v(t). With these functions the object mass can respond to the external request on x or v at a given time t. The object would only use performance as required. In java the mathematical functions would be placed in the methods
With these methods the gravity model could be extended to draw the two masses with a predefined refresh rate on a display. The refresh rate would directly control the CPU usage.
The behavior of objects is defined by their internal coding. After the creation of the object earth in the main method, mass and position are given. With the introduction of the moon at t0 the collision at tc can immediately be determined, although it could take a while until tc.
For this very general aspect of programming java supports the concept of events. In this context the basic idea is to pass the information of the collision at tc to a timer. The timer could wait until tc and the fire an Event and inform other objects via a Listener to give them a chance to react.