Object%20Orientated%20Programming%20in%20Perl - PowerPoint PPT Presentation

About This Presentation
Title:

Object%20Orientated%20Programming%20in%20Perl

Description:

... program explores the behavior of termites in a Java simulation. http://ccl. ... For this example our termites need to be able to walk around the grid and both ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 15
Provided by: megansh4
Category:

less

Transcript and Presenter's Notes

Title: Object%20Orientated%20Programming%20in%20Perl


1
Object Orientated Programming in Perl
  • Simulated Models of Termites

2
A Version of the Termite Model
  • This program explores the behavior of termites in
    a Java simulation.
  • http//ccl.northwestern.edu/netlogo/models/Termite
    s
  • To do this well we should take a look at a
    different kind of programming, Object Orientated
    programming.

3
What is an Object?
  • An object is a single entity that holds both the
    data for a part of a program and the code that
    works on that data.
  • This idea came from looking at how things are
    organized in the world. For example, a gene has a
    set of properties that define it and there is a
    set of actions that can be done to it.
  • In Perl object orientation is expressed in the
    form of classes that hold information about
    variables and methods.

4
An Object
5
Properties and Methods
  • Properties
  • Methods
  • Properties are the actual data values that tell
    you what exactly the object is.
  • For our example each termite needs to have
    information on where it is on the grid and
    whether or not it is holding a chip.
  • Methods are the things that you can do to your
    object.
  • For this example our termites need to be able to
    walk around the grid and both pick up and drop
    woodchips.

6
Objects in Perl
  • An object in perl is a reference to a special
    kind of hash, also called a class, and is marked
    by the class name.
  • Calling methods is different in OO programming
    also. This is done by first identifying the
    object to be acted upon and then specifying which
    method in that object you want to use.
  • In perl this is done with arrow notation (-gt). So
    to call the method step for our termite you would
    type termite-gtstep(args).

7
Constructors
  • To make a new object of the termite class you
    need to use a constructor. A constructor is a
    method inside the termite class called new.
  • This method takes in all of the arguments passed
    to the constructor and returns a scalar reference
    to a hash and puts the information from the
    arguments into the proper places in the hash.

8
Gene Example
  • Lets write a short program that makes a new
    gene and then asks for information about that
    gene.
  • We are going to need to write a class and the
    program that uses it.

9
The Termite Class
  • The termite wanders around randomly until it
    bumps into a wood chip.
  • If the termite is carrying a chip, it drops the
    chip and continues to wander.
  • If the termite is not carrying a chip, it picks
    up the one it bumped into and continues to wander.

10
The Grid
  • Perl doesnt particularly like two-dimensional
    arrays, but we need an area for our termites to
    run around and play.
  • To solve this problem lets just make a
    one-dimensional array that we know how to work
    with.

11
The Step Method
  • We need to add in a feature that will randomly
    pick a direction for the termite to step in.
  • The termite checks its location to see if there
    is a chip there. Lets add this in.

12
Object Orientation and Termites
  • Part 2

13
The Termite Class
  • What do you remember about our simulation from
    last week?
  • We need to finish up the logic for how the
    termite walks around the grid. We want the
    termite to immediately turn 180 degrees and take
    a step after each time it drops a chip. We also
    want to see what happens if we let the termite
    have the option of moving in all four cardinal
    directions as compared to just east and west.

14
Test it!
  • What would happen if after dropping a woodchip
    the termite it would randomly jump to an empty
    place on the grid?
  • How long do we want our termites to run around
    the grid? Lets try it out!
  • We can take the grid file that we saved at the
    end of the program and view it in Matlab to see
    where the termites moved our chips.
Write a Comment
User Comments (0)
About PowerShow.com