730 likes | 1.16k Views
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?.
E N D
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? The Walker School – Games and Simulations - 2010
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
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
Public Methods Right – click on a body. What public methods does it have? The Walker School – Games and Simulations - 2010
Inspecting Planet Properties Right click on a planet and inspect its properties. What is the mass? The Walker School – Games and Simulations - 2010
Space Public Methods What do the numbers mean in each case? The Walker School – Games and Simulations - 2010
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
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
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
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
Vectors dy dx Polar Representation = length and direction Cartesian Representation = dx and dy The Walker School – Games and Simulations - 2010
Vector Class Variables Open SmoothMover and Vector. What methods do they have? The Walker School – Games and Simulations - 2010
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
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
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
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
Add Movement The Walker School – Games and Simulations - 2010
Body Behavior Create multiple bodies. How do they behave? The Walker School – Games and Simulations - 2010
Add Movement (Left) How would you make the body move left? The Walker School – Games and Simulations - 2010
Solution: Left Movement Change the + to a - The Walker School – Games and Simulations - 2010
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
Importing Java Color Library The Walker School – Games and Simulations - 2010
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
Adding Gravitational Force The Walker School – Games and Simulations - 2010
Movement The Walker School – Games and Simulations - 2010
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
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
Greenfoot World Methods Which methods give us access to objects within the world? The Walker School – Games and Simulations - 2010
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
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
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
Apply Force – Part II The Walker School – Games and Simulations - 2010
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
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
Applying Gravity The Walker School – Games and Simulations - 2010
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
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
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
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
Acceleration Acceleration = Force / Mass The Walker School – Games and Simulations - 2010
Appling Gravity Method The Walker School – Games and Simulations - 2010
Trying it Out Try out the three initializing methods from Space object again. What do you observe? The Walker School – Games and Simulations - 2010
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
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
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
Obstacles, Gravity and Music The Walker School – Games and Simulations - 2010
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
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
Create Class Stub What are the basic methods we need to add to our stub? The Walker School – Games and Simulations - 2010