Presentation is loading. Please wait.

Presentation is loading. Please wait.

Newton’s Lab Games and Simulations O-O Programming in Java.

Similar presentations


Presentation on theme: "Newton’s Lab Games and Simulations O-O Programming in Java."— Presentation transcript:

1 Newton’s Lab Games and Simulations O-O Programming in Java

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

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 The Walker School – Games and Simulations - 2010 What happens when you activate one of these methods? Right-click on Space world at the top?

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

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

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

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

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

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

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

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 The Walker School – Games and Simulations - 2010 Open SmoothMover and Vector. What methods do they have?

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

15 Default Constructors The Walker School – Games and Simulations - 2010 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?

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

17 Constants The Walker School – Games and Simulations - 2010 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.

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

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

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

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

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

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

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

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 The Walker School – Games and Simulations - 2010 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.

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

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 The Walker School – Games and Simulations - 2010 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?

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

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 The Walker School – Games and Simulations - 2010 Use an if-statement so that gravity is applied to all objects (bodies) in the list except the current object. Refers to the current object.

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 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 Why do these numbers need to be doubles?

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

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

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 The Walker School – Games and Simulations - 2010 Try out the three initializing methods from Space object again. What do you observe?

44 Experimenting with Gravity The Walker School – Games and Simulations - 2010 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.

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

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 The Walker School – Games and Simulations - 2010 Improvements new Obstacle class modified Body class increased Gravity fixed obstacles more planets

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

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

51 Create a Constructor The Walker School – Games and Simulations - 2010 What parameters do we want to pass into the constructor?

52 Add Parameters to Constructor The Walker School – Games and Simulations - 2010 1.We want to know when a body hits the obstacle. 2.When it does we want to play a sound.

53 Add Play Sound Method The Walker School – Games and Simulations - 2010 Use Audacity to create a sound that you want to play when the planet hits the obstacle. What folder in the project, does this sound go?

54 Add an Act Method Stub The Walker School – Games and Simulations - 2010

55 Adding Obstacles The Walker School – Games and Simulations - 2010What type of data structure is this?

56 Act To Do List public void act { call the intersecting object method from Actor use a conditional statement to determine if the object has collided with the obstacle if touched, set the appropriate image play sound if not touched, set the appropriate image } The Walker School – Games and Simulations - 2010

57 Functioning Act Method The Walker School – Games and Simulations - 2010

58 Increase the Gravity Constant The Walker School – Games and Simulations - 2010 Gravity has been added to make the “bodies” move faster. What functionality has been added to slow them down?

59 Slowing Fast Bodies The Walker School – Games and Simulations - 2010

60 Create Random Bodies Stub What parameter(s) do we want to pass in order to create a number of bodies? The Walker School – Games and Simulations - 2010 Don’t forget to call this method in the act method.

61 Random Bodies To Do List write a method stub{ create a loop make a body of random size make a body of random mass (size * #?) give the body a random direction give the body a random speed give the body a random location (within the world) give the body a random color (r, g, b) create a new instance of the body (pass the parameters) } The Walker School – Games and Simulations - 2010

62 Create Random Bodies Method The Walker School – Games and Simulations - 2010

63 Bouncing Bodies To Do List write a method stub{ get the body’s location determine if the body is at the edge of the world cast the location to decimals (for movement) change the direction of movement, either in vertical or horizontal plane decelerate the body } The Walker School – Games and Simulations - 2010

64 Reflecting Bodies at World Edge Don’t forget to add the bounceAtEdge() method call in act() method. The Walker School – Games and Simulations - 2010

65 Activities – Part I Change the number of bodies that are created by default in this scenario. Create a different arrangement of obstacles in your scenario. Use different sounds (sound files) for your obstacles. The Walker School – Games and Simulations - 2010

66 Changing the Number of Bodies Under Space class change the number of random bodies created. The Walker School – Games and Simulations - 2010

67 Different Obstacle Arrangement The Walker School – Games and Simulations - 2010 Creates a random location along they y-axis.

68 Fixed Obstacle Arrangement The Walker School – Games and Simulations - 2010

69 Different Sounds The Walker School – Games and Simulations - 2010 Changes the set of sounds from piano to trumpet sounds. These 1-minute notes were recorded by band members in Audacity then stored in the project’s sound folder. We added the prefix of “tp” so we didn’t have to change any of the names in the soundFiles [] list.

70 Activities – Part II Use different images for your obstacles. Make planets change color every time the bounce off the edge of the universe. Make planets change color every time they hit an obstacle. Make a different kind of obstacle that gets switched on and off by being hit. When on, it continuously blinks and produces a sound at fixed intervals. The Walker School – Games and Simulations - 2010

71 Change the Obstacle Image Directions 1.Right-click on Obstacle class 2.Choose Set-Image 3.Choose the new image from the Greenfoot image library. Make your own image in Photoshop and add it to the image folder for the project. The Walker School – Games and Simulations - 2010

72 Programming Challenge Comets and asteroids move through the solar system. Create an image for one of these using Photoshop and add it to the world as an obstacle. To increase the difficulty of the challenge, have the object appear periodically at different speeds and if a ball hits this object, have it make a special sound. The Walker School – Games and Simulations - 2010

73 Activities – Part III Add some keyboard control. For example, pressing the right arrow key could add a small force to the right to all Body objects. Allow adding more planets. A mouse click into the universe while it is running should create a new planet at that location. The Walker School – Games and Simulations - 2010


Download ppt "Newton’s Lab Games and Simulations O-O Programming in Java."

Similar presentations


Ads by Google