Adding gravitational force

6.6 Adding gravitational force

Let us start by looking at the current act method in our Body class (Code 6.3). (If you have not done Exercise 6.12, then the call to the move method will not be there—you can add it now.)

88 Chapter 6 | ■ Interacting objects: Newton’s Lab

Code 6.3 The current act

/** method

* Act. That is: apply the gravitation forces from * all other bodies around, and then move. */

public void act() {

move();

While the code currently contains only the move call, the comment actually describes correctly what we have to do: Before we move, we should apply the forces caused by the gravitational pull of all other objects in space.

We can give an outline of the task in pseudo-code:

apply forces from other bodies :

get all other bodies in space ; for each of those bodies: {

apply gravity from that body to our own ; }

Since this is not an easy thing to do, we first start by making a separate method for this task (Code 6.4). Creating a separate (initially empty) method might seem like a trivial task at first that does not seem to accomplish much, but it greatly helps in breaking down our problem into smaller subproblems, and it helps structuring our thoughts.

Code 6.4 Preparing to apply

/** forces of gravity

* Act. For a body, that is: apply all the gravitation forces from * all other bodies around, and then move. */

public void act() {

applyForces(); move();

* Apply the forces of gravity from all other celestial bodies in * this universe. */

private void applyForces() {

// work to do here

6.6 Adding gravitational force

Concept: Note: Private methods

Private methods

are only visible The method we created in Code 6.4 has the keyword private at the beginning of its signature,

from within the not public as we have previously seen. class they are

Methods can be declared in.

public or private. When methods are intended to be called from outside They are used

the class (either interactively by a user or from another class), then they must be public. When to improve the

methods are intended to be called only from other methods within the same class (as is the structure of

case here), then they should be declared private. the code.

Private methods are not visible or accessible from outside the class itself. It is good practice to make methods private that are only intended for internal use. This helps preventing errors and documents the purpose of the method more clearly.

Next, we have to work out how we get access to all other objects in our world. The World class in Greenfoot has methods that give us access to objects within it.

Exercise 6.18 Look up Greenfoot’s World class in the Greenfoot class documentation. Find all methods that give us access to objects within the world. Write them down.

The most interesting of those methods for us is this one:

java.util.List getObjects(java.lang.Class cls)

This method gives us a list of all objects in the world of a particular class. The parameter to this method is of type java.lang.Class (i.e., the class named “ Class ” from the java.lang

package 2 ). We have seen parameters of this type before, in Chapter 4, when we used the canSee and eat methods in the Crab class to eat the worms. We can use a call to this method to get, for instance, a list of all Body objects in our world:

getObjects(Body. class )

We can also provide null as a parameter to receive a list of all objects of any class in the world:

Concept: getObjects( null ) The keyword null

The keyword null is a special expression that means nothing, or no object. By using it in a

stands for “nothing”

parameter list, we pass no object as the parameter. The value null could also be assigned to

or “no object”.

variables. The getObjects method is, however, a method of the World class, so it must be called on a

World object. We will write our code in the Body class, so we must first obtain the world object

2 The java.lang package is special: It contains the most commonly used classes, and classes in it are auto- matically imported. So we do not need to write an import statement for any class in java.lang.

90 Chapter 6 | ■ Interacting objects: Newton’s Lab

to call this method on. Luckily, there is a method in the Actor class that gives us access to the world object. Its signature is

World getWorld()

Find this method in the Greenfoot class documentation and read its description. This method will return the world object, and we can then call the getObjects method on the

resulting object:

getWorld().getObjects(Body. class )

This code can be used from an actor to get all objects of class Body . Let us look more closely at the return type now.

The return type of the getObjects method is specified as java.util.List . This indicates that there is a type called List in the java.util package in the standard class library, and that we will get an object of this type as a result of this method.

The List type is worth a closer look.