Title: Problem Solving with Data Structures using Java: A Multimedia Approach
1Problem Solving with Data Structures using Java
A Multimedia Approach
- Chapter 14 Using an Existing Simulation Package
2Chapter Objectives
3Simulations
- 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)
4Simulations 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 thing knows 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.
5Continuous 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?
6Actors or Agents Those that act in the
simulations
- Actors or Agents 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.
7Resources
- 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)
8When an object has to wait
- What happens if you (or your proxy object) need a
resource and its not available? - You wait in a queue
- A list that is first-in-first-out (FIFO)
9A 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
10Aggregation
- Some objects are made up of other objects.
- Cars have engines
- People have livers and lungs
- These internal things are objects, too!
- Livers dont directly mess with the innards of
lungs! - We call this aggregation
- Putting references to some objects inside of
other objects.
11Generalization 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
12Start 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
13Objects 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
Classes
Objects
14Worlds 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
15Balloon Code and Doc
- Double-click on the Balloon class
- Select Documentation on the right
- You can switch between the source code and
documentation
16Documentation
- Starts with the class name
- Should be an uppercase letter
- Then the superclasses
- Then the class definition
- And a short description
- Author
- Constructors
- Methods
17Methods
- 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
18Open the Wombat Scenario
You may need to click the Compile button to
update everything if classes looked hashed
19Make a Wombat, and some Leaves
Right click on Wombat class, Choose New Wombat()
Now, have the wombat act()
20Current Wombat act() method
Can you see why the Wombat wont necessarily find
all leaves?
21Create a new method for random walk
22New Wombat act() method
23Greenfoot worlds
- World class
- Abstract
- Subclass it to create a new class
- Actor class
- Subclass it to create Wombats and Balloons
24WombatWorld Class
25(No Transcript)
26(No Transcript)
27Making a Wall class
28Use the randomLeaves method as starting place
29New randomWombats
30New randomWalls
31New WombatWorld Constructor
32New Wombat act() so random turns
33Teach wombats to avoid walls
34Questions
- 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?
35MakingBreakout
http//www.greenfoot.org/scenarios/files/breakout.
zip
36Making 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 /
37- 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
38Constructor
39setUpBreakout()
40Create and set background image
41setUpBricks()
42Creating 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 - _at_param theColor the color to use for this
brick - /
- public Brick(Color theColor)
-
- color theColor
- updateImage()
-
-
43Constructor 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()
-
-
44What 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)
-
-
45Ball 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
46(No Transcript)
47(No Transcript)
48End of Ball class
49Making newBall in BreakoutWorld
50checkIfWon()
- /
- Method to check if the game is over and if
so tell the user - and stop the simulation
- /
- public void checkIfWon()
-
- List brickList this.getObjects(Brick.cla
ss) - if (brickList.size() 0)
-
- message.setText("You Won!!!!!!")
- Greenfoot.stop()
-
-
51Foxes and Rabbits
- An example predator and prey simulation
52Interactive Question
- In what method is it defined how far away a fox
looks for a rabbit?
53To-Do
- Make the fox look two cells away instead of one.
- What happens?
- How do you restore balance to the ecology?
- Make the fox look three cells away instead of
one. - What happens?
- How do you restore balance to the ecology?