Problem Solving with Data Structures using Java: A Multimedia Approach

Slides:



Advertisements
Similar presentations
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package Download greenfoot and install.
Advertisements

Breakout in Greenfoot Barb Ericson
Getting to know Greenfoot
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Program: Little Crab Mr Gano.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Pulling out a common superclass Barb Ericson Georgia Tech.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
Java: Chapter 1 Computer Systems Computer Programming II.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
Chapter 1 - Getting to know Greenfoot
Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java.
Greenfoot Game Programming Club.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Kakihijau.googlepages.com Introduction To Greenfoot Part-1.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Kakihijau.googlepages.com Introduction To Greenfoot Part-2.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Newton’s Lab Games and Simulations O-O Programming in Java.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 4 - Finishing the Crab Game
Clicker quiz 10/22/13 CSE 1102 Fall 2013.
Introduction to Object-oriented Programming
Chapter 4 - Finishing the Crab Game
OOP - Object Oriented Programming
Summary prepared by Kirk Scott
Object-Oriented Design
More Sophisticated Behavior
Classes and OOP.
Object Oriented Programming
Interlude 2 - The Greeps Competition
Inheritance and Polymorphism
Introduction To Greenfoot
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Barb Ericson Georgia Institute of Technology June 2007
Chapter 4: Writing Classes
Interfaces and Inheritance
Introduction to Object-oriented Program Design
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
Creating Games with Greenfoot
Can perform actions and provide communication
CSC 143 Inheritance.
The Little Crab Scenario
Can perform actions and provide communication
Organizing Memory in Java
Week 6 Object-Oriented Programming (2): Polymorphism
Games and Simulations O-O Programming in Java The Walker School
Breakout in Greenfoot Barb Ericson
Can perform actions and provide communication
Game Loop Update & Draw.
OO Design with Inheritance
Introduction to Object-Oriented Programming in Alice
Workshop for Programming And Systems Management Teachers
Chapter 14 Abstract Classes and Interfaces
Methods, Data and Data Types
Greenfoot November 8, 2009.
Classes CS 21a: Introduction to Computing I
More on Creating Classes
Programming games Share your plans for your virtual something.
Final and Abstract Classes
Chapter 4 System Modeling.
CMSC 202 Exceptions.
Presentation transcript:

Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package

Chapter Objectives

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

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 The Greenfoot Tutorial is always available from the Help menu.

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 Actor 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 Then the superclasses 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 Select “Source Code” for the Balloon to see the source code.

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?

Create a new method for random walk

New Wombat act() method

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

WombatWorld Class

Making a Wall class

Use the randomLeaves method as starting place

New randomWombats

New randomWalls

New WombatWorld Constructor

New Wombat act() so random turns

Teach wombats to avoid walls

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?

Making Breakout 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 */ Making the BreakoutWorld

public static final int RESOLUTION = 1; / 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;

Constructor

setUpBreakout()

Create and set background image

setUpBricks()

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); } What Bricks do

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; Ball class

End of Ball class

Making newBall in BreakoutWorld

checkIfWon() /** * 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.class); if (brickList.size() == 0) message.setText("You Won!!!!!!"); Greenfoot.stop(); }