html5-img
1 / 84

Chapter 6 - Interacting Objects: Newton’s Lab

Chapter 6 - Interacting Objects: Newton’s Lab. 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. Exercise 6.5 Space Class.

feo
Download Presentation

Chapter 6 - Interacting Objects: Newton’s Lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 - Interacting Objects: Newton’s Lab

  2. 6.1 The Starting Point: Newton’s Lab

  3. Exercise 6.1 Right Click on space

  4. Exercise 6.2 Sun and Planet

  5. Exercise 6.2 Sun and Two Planets

  6. Exercise 6.2 Sun, Planet, and Moon

  7. Exercise 6.3

  8. Exercise 6.4

  9. 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 {

  10. 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(); }

  11. 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); }

  12. 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); }

  13. 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); }

  14. Exercise 6.5 removeAllObjects () /** * Remove all objects currently in the world. */ private void removeAllObjects() { removeObjects (getObjects(Actor.class)); }

  15. 6.2 Helper Classes: SmoothMover and Vector http://www.greenfoot.org/programming/classes.html • Some Helper Classes • Counter • Explosion • Mover • Plotter • Rotator • Slider • Vector • Wander

  16. SmoothMover 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 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, . . . and so on. We will see the actor on screen at rounded x-coordinates.

  17. Abstract Classes public 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

  18. Exercise 6.6 accelerate addForce getExactX getExactY getMovement getSpeed move setLocation setLocation

  19. 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.

  20. 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.)

  21. Vectors dy dx Polar Representation = length and direction Cartesian Representation = dx and dy

  22. Exercise 6.8

  23. Exercise 6.8

  24. Exercise 6.9 Which methods can be called thru the object’s menu? Which methods cannot?

  25. Exercise 6.9 public 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

  26. 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

  27. 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.

  28. Code 6.1 public 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); }

  29. this this (20, 300, new Vector (90, 1.0), defaultColor ); 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. this.mass = mass; When we write this.mass, we specify that we mean the mass field of the current object.

  30. 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”

  31. Exercise 6.11 Does is Compile? Does it Execute? What does the Code do? What is its effect?

  32. Code 6.2 private 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.

  33. 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.

  34. 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.”

  35. Exercise 6.12 The Default Speed is 1.0 The Default Direction is 0 Degrees

  36. Exercise 6.13 Multiple Objects Move from Left to Right at a Constant Speed

  37. Exercies 6.14 The Sun Does Not Appear to Move The Earth Moves Straight Down Rapidly

  38. Exercise 6.14

  39. Exercise 6.14

  40. 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

  41. Exercise 6.15 -90 270 -180 180 0 360 -270 90

  42. 6.5 Using Java Library Classes import java.awt.Color; new Color (248, 160, 86) R G B java.awt Package Color Class

  43. java.awt.Color

  44. Exercise 6.16

  45. Exercise 6.17

  46. 6.6 Adding Gravitational Force 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; }

  47. Code 6.3 /** * Act. That is: apply the gravitation forces from * all other bodies around, and then move. */ public void act() { move (); }

  48. 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 }

  49. 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.

  50. Exercise 6.18

More Related