17-may-15 methods. overview in this presentation we will discuss these 4 topics: main method vs....
TRANSCRIPT
![Page 1: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/1.jpg)
Apr 18, 2023
Methods
![Page 2: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/2.jpg)
Overview
In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods Writing a Jeroo method Preconditions and postconditions
![Page 3: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/3.jpg)
Jeroo has many built-in methods
Actions hop, turn, …
Sensor methods isNet? isFlower? …
The programmer can define additional methods to extend the behavior of every Jeroo.
![Page 4: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/4.jpg)
Behaviors and methods
Behavior = an action that an object can take or a task it can perform in response to a request from an external source
Method = Collection of statements written in a programming language to describe a specific behavior.
1st define and name a behavior 2nd write code for the method
![Page 5: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/5.jpg)
Which parts of the program should be methods?
choose a behavior Any complex, well-defined step Any sequence of steps that more than 1 Jeroo will perform Any sequence of steps that occur several times.
A good behavior to turn into a method has a very clear definition and is used more than once in the program.
![Page 6: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/6.jpg)
Writing the method
A Jeroo method contains the code describing what any Jeroo needs to do to carry out a particular behavior.
method method_name ( ){
} //== method_name==
•Choose a name that is meaningful
•Empty parenthesis required on the header line (first line)
•Indent code inside the method
Body of method
Containing statements that describe how any arbitrary Jeroo can carry out this particular behavior.
![Page 7: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/7.jpg)
Important differences
Jeroo methods
Can be many other methods Choose your own names Must be called by the main
method Can’t instantiate in the
method
Main method
Only one main method Must be called main Always comes first Instantiate Jeroos
![Page 8: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/8.jpg)
Example of a Jeroo method
//***********************************************************
//Turn Around: makes a Jeroo turn 180 degrees//
***********************************************************
method turnAround( ){
turn(LEFT);turn(LEFT);
} // === end turnAround ===
Good , descriptive name for the method
The code is indented
Comments clearly explain what the method does
Notice that no Jeroo name is specified in the steps
Everything in yellow is good!
![Page 9: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/9.jpg)
To call the new method
method main(){
// create a jeroo and have it use the method
Jeroo Nancy = new Jeroo();
Nancy.turnAround();
}
![Page 10: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/10.jpg)
Important difference
Jeroo methods
Define a behavior available to ALL Jeroos so do not specify which Jeroo performs the steps inside the method
Example in a Jeroo method for any Jeroo
pick(); hop(2); give(AHEAD);
Main method
All steps must specify which Jeroo does each action:objectname.method();
Example in main for a Jeroo named betty
betty.pick(); betty.hop(2); betty.give(AHEAD);
![Page 11: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/11.jpg)
Using a Jeroo method
After you write your code
Use it just like any other methodmethod main( ){
Jeroo ali = new Jeroo(5,5,8);ali.plantCorner( );
} // === main ===
method plantCorner( ){
plant();hop();turn(RIGHT);hop();plant();
} // === plant Corner ===
![Page 12: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/12.jpg)
One method can call another The plantThree method is used twice in the
plantRectangle methodmethod plantThree( ){ plant( ); hop( ); plant( ); hop( ); plant( );} //=== plantThree ===method plantRectangle( ){
plantThree( );moveDown( );plantThree( );
}//=== plantRectangle ===
This code is
missing
most of its
comments!Defined above
Must also be defined somewhere
A
B
![Page 13: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/13.jpg)
Preconditions and Postconditions
A complete definition for a behavior includes: Preconditions
Assumptions of what is true before the method is called Postconditions
What will be true after the method is finished
In Jeroo, the only things that change are: island cells (flower planted, net gone…) Jeroo attributes (location, number of flowers …)
![Page 14: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/14.jpg)
Things to consider when writing a precondition
What must be true for this method to work? Ask: does the Jeroo need
A certain number of flowers? To be facing a particular direction? To be in a certain location?
Ask: are the contents of certain island cells important?
//********************************************//Plant 3 flowers in a row starting at current location//PRECONDITIONS:// 1. There are 2 clear spaces directly in front// 2. Jeroo has at least 3 flowers//********************************************method plantThree( ){…
![Page 15: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/15.jpg)
Things to consider when writing a postcondition
Describe what is true after the method finishes Ask: how will the method change
the number of flowers? the direction the Jeroo is facing? the Jeroo’s location?
Ask: have the contents of certain island cells changed?
// // POSTCONDITIONS:// 1. Three flowers have been planted, starting at the // beginning location and proceeding straight ahead// 2. The jeroo is standing on the last flower,
// facing its original direction// ********************************************method plantThree( ){ …
![Page 16: 17-May-15 Methods. Overview In this presentation we will discuss these 4 topics: Main method vs. Jeroo methods Choosing behaviors to turn into methods](https://reader036.vdocuments.us/reader036/viewer/2022072006/56649cfe5503460f949cf2da/html5/thumbnails/16.jpg)
The End