chapter 6 - interacting objects: newton’s lab
DESCRIPTION
Chapter 6 - Interacting Objects: Newton’s Lab. Bruce Chittenden. 6.1 The Starting Point: Newton’s Lab. Exercise 6.1. Right Click on space. Exercise 6.2. Sun and Planet. Exercise 6.2. Sun and Two Planets. Exercise 6.2. Sun, Planet, and Moon. Exercise 6.3. Exercise 6.4. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/1.jpg)
Chapter 6 - Interacting Objects: Newton’s Lab
Bruce Chittenden
![Page 2: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/2.jpg)
6.1 The Starting Point: Newton’s Lab
![Page 3: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/3.jpg)
Exercise 6.1
Right Click on space
![Page 4: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/4.jpg)
Exercise 6.2
Sun and Planet
![Page 5: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/5.jpg)
Exercise 6.2
Sun and Two Planets
![Page 6: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/6.jpg)
Exercise 6.2
Sun, Planet, and Moon
![Page 7: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/7.jpg)
Exercise 6.3
![Page 8: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/8.jpg)
Exercise 6.4
![Page 9: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/9.jpg)
Exercise 6.5 Space Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)import java.awt.Color;
/** * Space. The final frontier. * * @author Michael Kolling * @version 1.0 */public class Space extends World{
![Page 10: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/10.jpg)
Exercise 6.5 Space Constructor
/** * Create space. */ public Space() { super(960, 620, 1); // Uncomment one of the following method calls // if you want the objects created automatically: //sunAndPlanet(); //sunAndTwoPlanets(); //sunPlanetMoon(); }
![Page 11: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/11.jpg)
Exercise 6.5 SunAndPlanet ()
/** * Set up the universe with a sun and a planet. */ public void sunAndPlanet() { removeAllObjects(); addObject (new Body (50, 240.0, new Vector(270, 0.03), new Color(255, 216, 0)), 460, 270); addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 260); }
![Page 12: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/12.jpg)
Exercise 6.5 sunAndTwoPlanets ()
/** * Set up the universe with a sun and two planets. */ public void sunAndTwoPlanets() { removeAllObjects(); addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 310); addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 300); addObject (new Body (24, 4.6, new Vector(270, 1.8), new Color(248, 160, 86)), 180, 290); }
![Page 13: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/13.jpg)
Exercise 6.5 sunPlanetMoon ()
/** * Set up the universe with a sun, a planet, and a moon. */ public void sunPlanetMoon() { removeAllObjects(); addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270); addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260); addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260); }
![Page 14: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/14.jpg)
Exercise 6.5 removeAllObjects ()
/** * Remove all objects currently in the world. */ private void removeAllObjects() { removeObjects (getObjects(Actor.class)); }
![Page 15: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/15.jpg)
6.2 Helper Classes: SmoothMover and Vector
Some Helper Classes•Counter•Explosion•Mover•Plotter•Rotator•Slider•Vector•Wander
http://www.greenfoot.org/programming/classes.html
![Page 16: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/16.jpg)
SmoothMover
12.3, 12.9, 13.5, 14.1, 14.7, 15.3, 15.9, 16.5, 17.1, . . .
12, 13, 14, 14, 15, 15, 16, 17, 17, . . .
SmoothMover can, for example, have the x-coordinate 12.3. If we now move this actor along the x-coordinate in increments of 0.6, its successive locations will be
and so on. We will see the actor on screen at rounded x-coordinates.
![Page 17: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/17.jpg)
Abstract Classespublic abstract class SmoothMover extends Actor{ private Vector movement; private double exactX; private double exactY; public SmoothMover() { this(new Vector()); } /** * Create new thing initialised with given speed. */ public SmoothMover(Vector movement) { this.movement = movement; }
Cannot Create Objects for This Class, No Constructor
![Page 18: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/18.jpg)
Exercise 6.6
accelerate addForcegetExactXgetExactYgetMovementgetSpeedmovesetLocationsetLocation
![Page 19: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/19.jpg)
Exercise 6.7 /** * Set the location using exact (double) co-ordinates. */ public void setLocation(double x, double y) { exactX = x; exactY = y; super.setLocation((int) x, (int) y); } /** * Set the location of this actor. Redefinition of the standard Greenfoot * method to make sure the exact co-ordinates are updated in sync. */ public void setLocation(int x, int y) { exactX = x; exactY = y; super.setLocation(x, y); }
Can have the same name, as long as their parameters are different. This means that the methods (or constructors) have different signatures.
![Page 20: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/20.jpg)
Overloading
It is perfectly legal to have two methods that have the same name, as long as their parameter lists are different. This is called Overloading (The name of the method is Overloaded - it refers to more than one method.)
![Page 21: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/21.jpg)
Vectors
dy
dx
Polar Representation = length and directionCartesian Representation = dx and dy
![Page 22: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/22.jpg)
Exercise 6.8
![Page 23: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/23.jpg)
Exercise 6.8
![Page 24: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/24.jpg)
Exercise 6.9
Which methods can be called thru the object’s menu? Which methods cannot?
![Page 25: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/25.jpg)
Exercise 6.9public abstract class SmoothMover extends Actor{ private Vector movement; private double exactX; private double exactY; public SmoothMover() { this(new Vector()); } /** * Create new thing initialised with given speed. */ public SmoothMover(Vector movement) { this.movement = movement; }
smoothMover is not Callable since it is declared as an Abstract Class
![Page 26: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/26.jpg)
Exercise 6.10 /** * Construct a Body with default size, mass, movement and color. */ public Body() { this (20, 300, new Vector(0, 1.0), defaultColor); } /** * Construct a Body with a specified size, mass, movement and color. */ public Body(int size, double mass, Vector movement, Color color) { this.mass = mass; addForce(movement); GreenfootImage image = new GreenfootImage (size, size); image.setColor (color); image.fillOval (0, 0, size-1, size-1); setImage (image); }
Body Class has Two Constructors This is Another Example of Overloading
![Page 27: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/27.jpg)
6.3 The Existing Body Class
A constructor without any parameters is also called a Default Constructor
The Body class has two constructors. One constructor has no parameters and the other constructor has four parameters. The default constructor makes it easy for us to create bodies interactively without having to specify all the details.
![Page 28: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/28.jpg)
Code 6.1public class Body extends SmoothMover{
/** * Construct a Body with default size, mass, movement and color. */ public Body() { this (20, 300, new Vector(0, 1.0), defaultColor); } /** * Construct a Body with a specified size, mass, movement and color. */ public Body(int size, double mass, Vector movement, Color color) { this.mass = mass; addForce(movement); GreenfootImage image = new GreenfootImage (size, size); image.setColor (color); image.fillOval (0, 0, size-1, size-1); setImage (image); }
![Page 29: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/29.jpg)
this
this (20, 300, new Vector (90, 1.0), defaultColor );
this.mass = mass;
This line looks almost like a method call, except it uses the keyword this instead of a method name. Using this the constructor executes the other constructor, the one with parameters.
When we write this.mass, we specify that we mean the mass field of the current object.
![Page 30: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/30.jpg)
Exercise 6.11 /** * Construct a Body with default size, mass, movement and color. */ public Body() { this (20, 300, new Vector(0, 1.0), defaultColor); } /** * Construct a Body with a specified size, mass, movement and color. */ public Body(int size, double mass, Vector movement, Color color) { this.mass = mass; addForce(movement); GreenfootImage image = new GreenfootImage (size, size); image.setColor (color); image.fillOval (0, 0, size-1, size-1); setImage (image); }
Remove the “this”
![Page 31: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/31.jpg)
Exercise 6.11
Does is Compile?Does it Execute?What does the Code do?What is its effect?
![Page 32: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/32.jpg)
Code 6.2private static final double GRAVITY = 5.8;private static final Color defaultColor = new Color(255, 216, 0);
The term final defines this field to be a constant. A constant has similarities to a field, in that we can use the name in our code to refer to its value, but the value can never change (it is constant).
The effect of the static keyword is that this constant is shared between all actors of this class.
![Page 33: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/33.jpg)
6.4 First Extension: Creating Movement
The first obvious experiment is to make bodies move.
SmootherMover Class has a move () method and since Body is a SmoothMover, it too has access to this method.
![Page 34: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/34.jpg)
Exercise 6.12
/** * Act. That is: apply the gravitation forces from * all other bodies around, and then move. */ public void act() { move(); }
Add the move () Method to the act () Method of the Body Class.”
![Page 35: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/35.jpg)
Exercise 6.12
The Default Direction is 0 Degrees
The Default Speed is 1.0
![Page 36: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/36.jpg)
Exercise 6.13
Multiple Objects Move from Left to Right at a Constant Speed
![Page 37: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/37.jpg)
Exercies 6.14
The Sun Does Not Appear to MoveThe Earth Moves Straight Down Rapidly
![Page 38: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/38.jpg)
Exercise 6.14
![Page 39: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/39.jpg)
Exercise 6.14
![Page 40: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/40.jpg)
Exercise 6.15
/** * Construct a Body with default size, mass, movement and color. */ public Body() { this (20, 300, new Vector(-180, 1.0), defaultColor); }
Change the Direction in the Default Constructor from 0 to -180
![Page 41: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/41.jpg)
Exercise 6.15
0360
-27090
-90270
-180180
![Page 42: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/42.jpg)
6.5 Using Java Library Classes
import java.awt.Color;
new Color (248, 160, 86)
R G B
java.awt Package
Color Class
![Page 43: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/43.jpg)
java.awt.Color
![Page 44: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/44.jpg)
Exercise 6.16
![Page 45: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/45.jpg)
Exercise 6.17
![Page 46: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/46.jpg)
6.6 Adding Gravitational Force
apply forces from other bodies: get all other bodies in space; for each of those bodies: { apply gravity from that body to our own; }
We can give an outline of the task in pseudo-code.
![Page 47: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/47.jpg)
Code 6.3
/** * Act. That is: apply the gravitation forces from * all other bodies around, and then move. */public void act() { move ();}
![Page 48: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/48.jpg)
Code 6.4/* * 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}
![Page 49: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/49.jpg)
Private Methods
Methods can be public or private. When methods are intended to be called from outside the class (either interactively by a user or from another class), the they must be public. When methods are intended to be called only from other methods within the same class, then they should be declared private.
![Page 50: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/50.jpg)
Exercise 6.18
![Page 51: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/51.jpg)
Exercise 6.18
getObjects ( java.lang.Class cls )getObjectsAt ( int x, int y, java.lang.Class cls )numberOfObjects ( )removeObject ( Actor object)removeObjects ( java.util.Collection objects )
![Page 52: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/52.jpg)
getObjects ()
java.util.List getObjects ( java.lang.Class cls )Gives a list of all objects in the world of a particular class
getObjects ( Body.class )Gets a list of all the objects in the world of class Body
getObjects ( null )The keyword null is a special expression that means nothing, or no object
![Page 53: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/53.jpg)
getWorld().getObjects
getWorld().getObjects ( Body.class )getObjects is 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 to call this method on.
World getWorld()There is a method in the Actor class that gives us access to the World class. It signature isWorld getWorld ().
![Page 54: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/54.jpg)
6.7 The List Type
The List type is not a class, but an interface. Interfaces are a Java construct that provides an abstraction over different possible implementing classes.
java.util.List
![Page 55: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/55.jpg)
Exercise 6.19
![Page 56: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/56.jpg)
Exercise 6.19
add ( E o )add ( int index, E element )addAll ( Collection <? extends E> c )addAll ( int index, Collection< ? extends E> c )remove ( int index )remove ( Object o )size ()
![Page 57: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/57.jpg)
Exercise 6.20
Interface List<E>
![Page 58: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/58.jpg)
Interface List<E>
Interface List<E>Interface is in the place of class and the notation <E> is after the type name. This is the Generic Type. This means that the type List needs an additional type specified as a parameter. This second type specifies the type of the elements held within the list.
![Page 59: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/59.jpg)
Interface List<E>List<String>
A list of Strings
List<Actor>A list of Actors
List<Body> bodiesA list of bodies
List<Body> bodies = getWorld().getobjects (Body.class):The variable bodies holds a list of all the bodies that exist in the World.
![Page 60: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/60.jpg)
Code 6.5
import java.util.List;
/* * Apply the forces of gravity from all other celestial bodies in this universe */private void applyForces() { List<Body> bodies = getWorld().getObjects(Body.class);}
![Page 61: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/61.jpg)
6.8 The for-each Loop
for ( Element Type variable : collection ){ statements;}
Java has a specialized loop for stepping through every element of a collection. It is called a for-each loop .
![Page 62: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/62.jpg)
The for-each Loop
for ( Body body : bodies ){ body.move();}
for each body in bodies do: body = first element from ‘bodies’; execute loop statements; body = second element for ‘bodies’; execute loop statements; body = third element from ‘bodies’; execute loop statements; . . .
![Page 63: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/63.jpg)
Code 6.6/* * Apply the forces of gravity from all other celestial bodies in this universe */private void applyForces() { List<Body> bodies = getWorld().getObjects(Body.class); for (Body body : bodies) { if (body != this) { applyGravity (body); } }}
/* * Apply the gravity force of a given body to this one. */ private void applyGravity (Body other){ // work to do here}
![Page 64: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/64.jpg)
6.9 Applying Gravity
Newton's Law of Universal Gravitation states that every massive particle in the universe attracts every other massive particle with a force which is directly proportional to the product of their masses and inversely proportional to the square of the distance between them.
![Page 65: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/65.jpg)
Code 6.7/* * Apply the gravity force of a given body to this one. */ private void applyGravity(Body other){ double dx = other.getExactX() - this.getExactX(); double dy = other.getExactY() - this.getExactY(); Vector force = new Vector (dx, dy); double distance = Math.sqrt (dx*dx + dy*dy); double strength = GRAVITY * this.mass * other.mass / (distance * distance); double acceleration = strength / this.mass; force.setLength (acceleration); addForce (force);}
![Page 66: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/66.jpg)
Pythagorean Theorem
a2 + b2 = c2
![Page 67: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/67.jpg)
Exercise 6.21
![Page 68: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/68.jpg)
Math Class sqrt
![Page 69: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/69.jpg)
Exercise 6.22
max ( int a, int b )
![Page 70: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/70.jpg)
Exercise 6.22
![Page 71: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/71.jpg)
Acceleration
acceleration =forcemass
Once we have calculated the acceleration, we can set our force vector to the correct length and add this vector to the movement of our body.
![Page 72: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/72.jpg)
Exercise 6.23
a2 + b2 = c2
dx2 + dy2 = distance2
(other.getExactX()-this.getExactX())2 + (other.getExactY()-this.getExactY())2 = distance2
distance = Math.sqrt ( (other.getExactX()-this.getExactX())2 + (other.getExactY()-this.getExactY())2 )
![Page 73: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/73.jpg)
Exercise 6.23
force = (mass1 * mass2 / distance2 ) * G
force = ( this.mass * other.mass / distance *distance ) * GRAVITY
force = mass1 X mass2distance
G
![Page 74: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/74.jpg)
Exercise 6.24
![Page 75: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/75.jpg)
Exercise 6.24
![Page 76: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/76.jpg)
Exercise 6.24
![Page 77: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/77.jpg)
Exercise 6.25
If the Gravitational Constant is set Higher than 5.8, planets spiral into one another, and if it is set lower, planets fly off into deep space.
![Page 78: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/78.jpg)
Exercise 6.26
size mass vector movement x yr g b
![Page 79: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/79.jpg)
Exercise 6.26
addObject (new Body (100, 500.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270); addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260); addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260);
![Page 80: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/80.jpg)
Exercise 6.27
All Systems That I Tested Were Very Unstable
![Page 81: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/81.jpg)
6.11 Gravity and MusicThe idea to add sound to a gravity project was inspired by Kepler’s Orrery (see https://keplers-orrery.dev.java.net/ )
![Page 82: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/82.jpg)
Exercise 6.28
![Page 83: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/83.jpg)
6.12 Summary of Programming Techniques
One of the most important topics in the chapter was the use of additional classes from the Standard Java Class Library, Color, Math, and List.
Another new addition was the use of a new loop the for-each loop. This loop is used to do something to every element of a Java collection such as a List. If we need an index, or a loop independent of a collection, then we must use a for loop or a while loop instead.
![Page 84: Chapter 6 - Interacting Objects: Newton’s Lab](https://reader036.vdocuments.mx/reader036/viewer/2022081420/5681677d550346895ddc82e2/html5/thumbnails/84.jpg)
Concept Summary