1 / 73

Newton’s Lab

Newton’s Lab. Games and Simulations O-O Programming in Java The Walker School. The Walker School – Games and Simulations - 2010. Space, The Final Frontier. You can create new bodies. What kinds of bodies are in our solar system? Why don’t we see a class for each type?.

gilda
Télécharger la présentation

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. Newton’s Lab Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations - 2010

  2. Space, The Final Frontier You can create new bodies. What kinds of bodies are in our solar system? Why don’t we see a class for each type? The Walker School – Games and Simulations - 2010

  3. Solar System • The main types of bodies in our solar system contains planets, planetoids, moons, asteroids and comets. While each of these varies in size, mass, composition and velocity, they are also similar in many ways. Because each object does have a size, mass, and velocity, we can use abstraction in programming to create a Body Class that handles each of these items as variables. The Walker School – Games and Simulations - 2010

  4. 3 Basic Methods Right-click on Space world at the top? What happens when you activate one of these methods? The Walker School – Games and Simulations - 2010

  5. Public Methods Right – click on a body. What public methods does it have? The Walker School – Games and Simulations - 2010

  6. Inspecting Planet Properties Right click on a planet and inspect its properties. What is the mass? The Walker School – Games and Simulations - 2010

  7. Space Public Methods What do the numbers mean in each case? The Walker School – Games and Simulations - 2010

  8. Helper Classes Also known as Abstract classes. They do not have constructors, so you can’t create instances of them. 2 Support Classes SmoothMover Vector The Walker School – Games and Simulations - 2010

  9. Helper (or Support) Classeshttp://www.greenfoot.org/programming/classes.html Some Helper Classes • Counter • Explosion • Mover • Plotter • Rotator • Slider • Vector • Wander The Walker School – Games and Simulations - 2010

  10. Classes Inherited from SmoothMover How are these 2 classes different? What classes are inherited from Vector? Why is it not under Actor? The Walker School – Games and Simulations - 2010

  11. Overloading Can have the same name, as long as their parameters are different. This means that the methods (or constructors) have different signatures. Has 2 constructers “Body” with different parameters. The Walker School – Games and Simulations - 2010

  12. Vectors dy dx Polar Representation = length and direction Cartesian Representation = dx and dy The Walker School – Games and Simulations - 2010

  13. Vector Class Variables Open SmoothMover and Vector. What methods do they have? The Walker School – Games and Simulations - 2010

  14. Method Inheritance Open SmoothMover and Vector. Which methods can you call from the object menu? Which can’t you call? The Walker School – Games and Simulations - 2010

  15. Default Constructors Does not have any parameters. Makes it easy to create bodies interactively without having to specify details. This is a default constructor. Is this an example of overloading? The Walker School – Games and Simulations - 2010

  16. this. Only possible in constructors. Used to execute another constructor. What happens when you remove this. in front of the word mass? Does it compile? The Walker School – Games and Simulations - 2010

  17. Constants Means the value never changes. Means it’s shared between all actors of this class. These variables are constants. These variables do not change in a program. The Walker School – Games and Simulations - 2010

  18. Add Movement The Walker School – Games and Simulations - 2010

  19. Body Behavior Create multiple bodies. How do they behave? The Walker School – Games and Simulations - 2010

  20. Add Movement (Left) How would you make the body move left? The Walker School – Games and Simulations - 2010

  21. Solution: Left Movement Change the + to a - The Walker School – Games and Simulations - 2010

  22. Java Packages Programmers typically use packages to organize classes belonging to the same category or providing similar functionality. import java.awt.Color; Imported using dot notation. The Walker School – Games and Simulations - 2010

  23. Importing Java Color Library The Walker School – Games and Simulations - 2010

  24. Default Color of Body Sets the RGB values Calls the Color class. What is the legal range for colors? The Walker School – Games and Simulations - 2010

  25. Adding Gravitational Force The Walker School – Games and Simulations - 2010

  26. Movement The Walker School – Games and Simulations - 2010

  27. Apply Force To Do List Apply forces from other bodies: get all other bodies in space; for each of those bodies:{ apply gravity from that body to our own; } The Walker School – Games and Simulations - 2010

  28. Apply Force – Part I Methods only intended to be called from within the class can be labeled private. When methods are intended to be called from outside the class, they should be labeled public. The Walker School – Games and Simulations - 2010

  29. Greenfoot World Methods Which methods give us access to objects within the world? The Walker School – Games and Simulations - 2010

  30. Getting Objects • Java.util.List getObjects(java.lang.Class cls) • Gives a list of all objects in the world of a particular class. • getObjects(Body.class) • Calls the objects in the world. • getObjects(null) • Keyword null is a special expression that means nothing, or no object. • getWorld().getObjects(Body.class) • Can be used from an actor to get all objects of class Body. The Walker School – Games and Simulations - 2010

  31. Java.util.list What methods can be used to add items to a list? What methods can be used to remove items to a list? What methods can be used to found out how many items are in a list? The Walker School – Games and Simulations - 2010

  32. Lists are not Classes, but Interfaces generic type – need to put the type of list in the brackets, such as String. The Walker School – Games and Simulations - 2010

  33. Apply Force – Part II The Walker School – Games and Simulations - 2010

  34. For Loops Helps to step through the items in a collection, or list. for (ElementType variable : collection) { statements; } The Walker School – Games and Simulations - 2010

  35. Apply Force – Part III Refers to the current object. Use an if-statement so that gravity is applied to all objects (bodies) in the list except the current object. The Walker School – Games and Simulations - 2010

  36. Applying Gravity The Walker School – Games and Simulations - 2010

  37. Newton’s Law 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. The Walker School – Games and Simulations - 2010

  38. Apply Gravity To Do List Why do these numbers need to be doubles? Apply gravity from an object { get the location of the objects in space; calculate the distance between each of the objects; create a new vector using this data to determine direction; calculate the distance between the two bodies using the Pythagoras theorem; use Newton's formula to calculate the strength of the force calculate the acceleration; add the force; } The Walker School – Games and Simulations - 2010

  39. Pythagorean Theorem Look up the class Math in the Java API. How many parameters does the sqrt method have? The Walker School – Games and Simulations - 2010

  40. Square Root Now, find the method that can be used to find the maximum of two integers. What is it called? The Walker School – Games and Simulations - 2010

  41. Acceleration Acceleration = Force / Mass The Walker School – Games and Simulations - 2010

  42. Appling Gravity Method The Walker School – Games and Simulations - 2010

  43. Trying it Out Try out the three initializing methods from Space object again. What do you observe? The Walker School – Games and Simulations - 2010

  44. Experimenting with Gravity Change the gravity constant at the top of the body class. What happens? Change it by ½, change it by doubling it; change it by 1, change it by a decimal. The Walker School – Games and Simulations - 2010

  45. Experimenting with Mass & Movement Experiment with changes to the mass and/or initial movement of the bodies defined in the Body class. What happens? size mass vector movement r g b x y When experimenting it’s important to change one variable at a time. The Walker School – Games and Simulations - 2010

  46. Programming / Research Challenge • Create some new set-ups of stars and planets and see how they interact. Can you come up with a system that is stable? With what you have programmed, could you create a model of our solar system? Is our solar systems stable? Explain your reasoning. The Walker School – Games and Simulations - 2010

  47. Obstacles, Gravity and Music The Walker School – Games and Simulations - 2010

  48. Improvements to Lab v. 3 Improvements • new Obstacle class • modified Body class • increased Gravity • fixed obstacles • more planets The Walker School – Games and Simulations - 2010

  49. Add Obstacle Class • Directions • Right click on Actor • Add new subClass • Name it “Obstacle” • Choose the orange block from “Objects” The Walker School – Games and Simulations - 2010

  50. Create Class Stub What are the basic methods we need to add to our stub? The Walker School – Games and Simulations - 2010

More Related