1 / 51

Problem Solving with Data Structures using Java: A Multimedia Approach

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.

pittd
Download Presentation

Problem Solving with Data Structures using Java: A Multimedia Approach

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. Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package

  2. Chapter Objectives

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

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

  5. 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?

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

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

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

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

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

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

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

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

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

  15. Balloon Code and Doc • Double-click on the Balloon class • Select Documentation on the right • You can switch between the source code and documentation

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

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

  18. 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?

  19. Open the Wombat Scenario You may need to click the Compile button to update everything if classes looked “hashed”

  20. Make a Wombat, and some Leaves Right click on Wombat class, Choose New Wombat() Now, have the wombat act()

  21. Current Wombat act() method Can you see why the Wombat won’t necessarily find all leaves?

  22. Create a new method for random walk

  23. New Wombat act() method

  24. Greenfoot worlds • World class • Abstract • Subclass it to create a new class • Actor class • Subclass it to create Wombats and Balloons

  25. WombatWorld Class

  26. Making a Wall class

  27. Use the randomLeaves method as starting place

  28. New randomWombats

  29. New randomWalls

  30. New WombatWorld Constructor

  31. New Wombat act() so random turns

  32. Teach wombats to avoid walls

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

  34. MakingBreakout http://www.greenfoot.org/scenarios/files/breakout.zip

  35. 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 */

  36. 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;

  37. Constructor

  38. setUpBreakout()

  39. Create and set background image

  40. setUpBricks()

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

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

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

  44. 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;

  45. End of Ball class

  46. Making newBall in BreakoutWorld

More Related