510 likes | 520 Views
Learn how to use an existing simulation package to create computer simulations that can provide a framework for understanding and analyzing complex systems. This chapter explores the concepts of continuous and discrete simulations, actors, resources, and modeling techniques such as aggregation and generalization.
E N D
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package
Simulations • “A simulation is a representation of a system of objects in a real or fantasy world.The purpose of creating a computer simulation is to provide a framework in which to understand the simulated situation, for example, to understand the behavior of a waiting line, the workload of clerks, or the timeliness of service to customers.A computer simulation makes it possible to collect statistics about these situations, and to test out new ideas about their organization.” • Adele Goldberg & David Robson, Smalltalk-80: The Language and Its Implementation (Addison-Wesley, 1989)
Simulations and Objects • Object-oriented programming was invented, in part, to make simulations easier to build! • The characteristics of objects make them more like real world objects, e.g., • Each thingknows some stuff and knows how to do some stuff. • Objects get things done by asking each other to do things. • Your internals are private, unless you want to make them otherwise.
Continuous vs. Discrete Simulations • Two main kinds of simulations in the world. • Continuous: Each moment of time is simulated. • When every moment counts. • Discrete: Skip to the important moments. • Want to simulate 100 years?
Actors: Those that act in the simulations • Actors do things, take time, and request and use resources. • In continuous simulations, actors are told to act(). • In discrete event simulations, actors do something, then reschedule themselves in the simulation.l
Resources • Resources are points of coordination in a simulation. • Examples: A cashier, a library book, a parking space on a ferry, a jelly bean. • Some resources are fixed and others are produced and consumed. • Some resources are renewable and shared. • Others are coordinated. • Example: For a surgeon to do a surgery, the patient must meet the surgeon at the operating table (the resource)
When an object has to wait… • What happens if you (or your proxy object) need a resource and it’s not available? • You wait in a queue • A list that is first-in-first-out (FIFO)
A simulation is an executed model • Setting up a simulation is a process of modeling the world (real or fantasy) to be simulated. • That model is realized in terms of objects. • We want our model to: • Reflect the world. • Be easy to extend and change. • Some of our modeling techniques: • Aggregation • Generalization and specialization
Aggregation • Some objects are made up of other objects. • Cars have engines • People have livers and lungs • These internal things are objects, too! • Livers don’t directly mess with the innards of lungs! • We call this aggregation • Putting references to some objects inside of other objects.
Generalization and Specialization • There are general and specialized forms of real world objects. • Cells are biological objects that have membranes and a nucleus and mitochondria and… • Blood, lung, and liver cells are all cells but have specialized functions. • The superclass-subclass relationship is a way of modeling general forms of objects and specialized forms of objects
Start Greenfoot • Click on the Greenfoot icon to start it • The first time it will ask if you want to do the tutorial • After that it will open the last scenario • Click on Scenario and open to open another one • Like balloons • Like wombat
Objects and Classes • Classes define what objects know and can do • There is one Balloon class • Objects do the action • There can be many objects of the same class • There are many balloon objects Objects Classes
Worlds and Actors • Greenfoot has two main classes • World • Place to hold and display actors • Has a width and height and cell size • Of type integer (int) • Has a background image • Actor • Actors know how to act • Actors have a x and y location in the world
Balloon Code and Doc • Double-click on the Balloon class • Select Documentation on the right • You can switch between the source code and documentation
Documentation • Starts with the class name • Should be an uppercase letter • Then the superclasses • Then the class definition • And a short description • Author • Constructors • Methods
Methods • Provide behavior for objects • Balloons can act and pop • public is the visibility • Who can see and execute this method • void is the return type • Nothing is returned from the method • The () means that the methods take no values
Text Questions: • Which method in what class adds all bombs and dart to the world? • What method in what class increases the score and by how much? • What does an explosion object do when it acts? • What does a balloon object do when it acts?
Open the Wombat Scenario You may need to click the Compile button to update everything if classes looked “hashed”
Make a Wombat, and some Leaves Right click on Wombat class, Choose New Wombat() Now, have the wombat act()
Current Wombat act() method Can you see why the Wombat won’t necessarily find all leaves?
Greenfoot worlds • World class • Abstract • Subclass it to create a new class • Actor class • Subclass it to create Wombats and Balloons
Questions • Does at least one wombat find the leaf? • How long does it take a wombat to find a leaf? • Can you make a smarter wombat? • Can you modify the scenario so that wombats can smell leaves that are within a certain radius of their current position?
MakingBreakout http://www.greenfoot.org/scenarios/files/breakout.zip
Making the BreakoutWorld import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.util.List; public class BreakoutWorld extends World { /////////////////// constants ////////////////////////// /** the width of the bricks in pixels (cells) */ public static final int BRICK_WIDTH = 36; /** the height of the bricks in pixels (cells) */ public static final int BRICK_HEIGHT = 8; /** the distance between bricks in pixels (cells) */ public static final int BRICK_SEP = 4; /** the number of bricks per row */ public static final int NUM_BRICKS_PER_ROW = 10; /** distance from the top edge in pixels (cells) */ public static final int BRICK_Y_OFFSET = 70; /** the number of pixels per cell */
public static final int RESOLUTION = 1; /** world width in pixels (cells) */ public static final int WIDTH = (BRICK_WIDTH + BRICK_SEP) * NUM_BRICKS_PER_ROW + BRICK_SEP; /** world height in pixels (cells) */ public static final int HEIGHT = 600; /** number of rows of bricks */ public static final int NUM_ROWS = 10; /** the colors to use for each row of bricks */ public static final Color[] colorArray = {Color.RED, Color.RED, Color.ORANGE, Color.ORANGE, Color.GREEN,Color.GREEN, Color.YELLOW,Color.YELLOW, Color.CYAN, Color.CYAN}; ////////////////// instance fields /////////////////////////////// /** the number of balls created in the game so far */ private int numBalls = 0; /** a message displayed for the user */ private Message message = null;
Creating the brick public class Brick extends Actor { /** the width of the brick */ private int width = 36; /** the height of the brick */ private int height = 8; /** the color of the brick */ private Color color; /////////// constructor //////////////// /** * Constructor that takes the color for the brick * @param theColor the color to use for this brick */ public Brick(Color theColor) { color = theColor; updateImage(); }
Constructor for Brick /** * Constructor that takes the width, height, and color for this * brick. */ public Brick(int theWidth, int theHeight, Color theColor) { width = theWidth; height = theHeight; color = theColor; updateImage(); }
What Bricks do /** * Method to act during a time step */ public void act() {} /** * Method to create the image and set it for this brick. * If you change the width, height, or color invoke this * method. */ public void updateImage() { GreenfootImage image = new GreenfootImage(width,height); image.setColor(this.color); image.fillRect(0,0,width,height); setImage(image); } }
Ball class public class Ball extends Actor { //////////// fields ///////////////////////////// /** the radius of this ball */ private int radius = 10; /** the width of this ball (diameter) */ private int width = radius * 2; /** the color of the ball */ private Color color = Color.BLACK; /** the amount of change in x during each act */ private int velX; /** the amount of change in y during each act */ private int velY = 3;