TRINITY CSCI 2323 - Object Orientated Programming in Perl

Unformatted text preview:

Object Orientated Programming in PerlA Version of the Termite ModelWhat is an Object?An ObjectProperties and MethodsObjects in PerlConstructorsGene ExampleThe Termite ClassThe GridThe Step MethodObject Orientation and TermitesSlide 13Test it!Object Orientated Object Orientated Programming in PerlProgramming in PerlSimulated Models of TermitesSimulated Models of TermitesA Version of the Termite A Version of the Termite ModelModelThis program explores the behavior of This program explores the behavior of termites in a Java simulation.termites in a Java simulation.http://ccl.northwestern.edu/netlogo/models/Tehttp://ccl.northwestern.edu/netlogo/models/TermitesrmitesTo do this well we should take a look at a To do this well we should take a look at a different kind of programming, Object different kind of programming, Object Orientated programming. Orientated programming.What is an Object?What is an Object?An object is a single entity that holds both the An object is a single entity that holds both the data for a part of a program and the code data for a part of a program and the code that works on that data. that works on that data. This idea came from looking at how things are This idea came from looking at how things are organized in the world. For example, a gene organized in the world. For example, a gene has a set of properties that define it and there has a set of properties that define it and there is a set of actions that can be done to it.is a set of actions that can be done to it.In Perl object orientation is expressed in the In Perl object orientation is expressed in the form of classes that hold information about form of classes that hold information about variables and methods.variables and methods.An ObjectAn ObjectProperties and MethodsProperties and MethodsPropertiesPropertiesProperties are the Properties are the actual data values that actual data values that tell you what exactly tell you what exactly the object is. the object is. For our example each For our example each termite needs to have termite needs to have information on where it information on where it is on the grid and is on the grid and whether or not it is whether or not it is holding a chip.holding a chip.MethodsMethodsMethods are the things Methods are the things that you can do to your that you can do to your object.object.For this example our For this example our termites need to be termites need to be able to walk around the able to walk around the grid and both pick up grid and both pick up and drop woodchips. and drop woodchips.Objects in PerlObjects in PerlAn object in perl is a reference to a special kind An object in perl is a reference to a special kind of hash, also called a class, and is marked by of hash, also called a class, and is marked by the class name. the class name. Calling methods is different in OO Calling methods is different in OO programming also. This is done by first programming also. This is done by first identifying the object to be acted upon and identifying the object to be acted upon and then specifying which method in that object then specifying which method in that object you want to use.you want to use.In perl this is done with arrow notation (->). So In perl this is done with arrow notation (->). So to call the method step for our termite you to call the method step for our termite you would type: $termite->step(args). would type: $termite->step(args).ConstructorsConstructorsTo make a new object of the termite class you To make a new object of the termite class you need to use a constructor. A constructor is a need to use a constructor. A constructor is a method inside the termite class called new.method inside the termite class called new.This method takes in all of the arguments passed This method takes in all of the arguments passed to the constructor and returns a scalar reference to the constructor and returns a scalar reference to a hash and puts the information from the to a hash and puts the information from the arguments into the proper places in the hash.arguments into the proper places in the hash.Gene ExampleGene ExampleLet’s write a short program Let’s write a short program that makes a new ‘gene’ that makes a new ‘gene’ and then asks for and then asks for information about that information about that gene.gene.We are going to need to We are going to need to write a class and the write a class and the program that uses it.program that uses it.The Termite ClassThe Termite ClassThe termite wanders around randomly until it The termite wanders around randomly until it bumps into a wood chip. bumps into a wood chip. If the termite is carrying a chip, it drops the If the termite is carrying a chip, it drops the chip and continues to wander.chip and continues to wander.If the termite is not carrying a chip, it picks up If the termite is not carrying a chip, it picks up the one it bumped into and continues to the one it bumped into and continues to wander.wander.The GridThe GridPerl doesn’t particularly Perl doesn’t particularly like two-dimensional like two-dimensional arrays, but we need an arrays, but we need an area for our termites to area for our termites to run around and play.run around and play.To solve this problem To solve this problem let’s just make a one-let’s just make a one-dimensional array that dimensional array that we know how to work we know how to work with.with.The Step MethodThe Step MethodWe need to add in a feature that will We need to add in a feature that will randomly pick a direction for the termite to randomly pick a direction for the termite to step in.step in.The termite checks its location to see if there The termite checks its location to see if there is a chip there. Let’s add this in. is a chip there. Let’s add this in.Object Orientation and Object Orientation and TermitesTermitesPart 2Part 2The Termite ClassThe Termite ClassWhat do you remember about our simulation What do you remember about our simulation from last week?from last week?We need to finish up the logic for how the We need to finish up the logic for how the termite walks around the grid. We want the termite walks around the grid. We want the termite to immediately turn 180 degrees and termite to immediately turn 180 degrees and take a step after each time it drops a chip. We take a step after each time it drops a chip. We also want to see what happens if we


View Full Document

TRINITY CSCI 2323 - Object Orientated Programming in Perl

Documents in this Course
Load more
Download Object Orientated Programming in Perl
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object Orientated Programming in Perl and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object Orientated Programming in Perl 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?