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

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
Interfaces.
Program: Little Crab Mr Gano.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
Chapter 1 - Getting to know Greenfoot Bruce Chittenden.
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
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,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Pulling out a common superclass Barb Ericson Georgia Tech.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
Java: Chapter 1 Computer Systems Computer Programming II.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 14: Using an Existing Simulation Package.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 1 - Getting to know Greenfoot
Classes CS 21a: Introduction to Computing I First Semester,
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Lecture 4. Greenfoot 1 Pablo Romero, Department of Informatics Greenfoot Programming simulations and games in Java.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Greenfoot Game Programming Club.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
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.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
Chapter 4 - Finishing the Crab Game
Summary prepared by Kirk Scott
More Sophisticated Behavior
Introduction To Greenfoot
Barb Ericson Georgia Institute of Technology June 2007
Introduction to Object-oriented Program Design
Creating Games with Greenfoot
The Little Crab Scenario
Problem Solving with Data Structures using Java: A Multimedia Approach
Games and Simulations O-O Programming in Java The Walker School
Breakout in Greenfoot Barb Ericson
Workshop for Programming And Systems Management Teachers
Methods, Data and Data Types
Greenfoot November 8, 2009.
Classes CS 21a: Introduction to Computing I
More on Creating Classes
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 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.

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 ClassesObjects

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

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

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 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 os/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;

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

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

Foxes and Rabbits An example predator and prey simulation:

Interactive Question: In what method is it defined how far away a fox looks for a rabbit?

To-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?