Methods Overview n In this presentation we will















- Slides: 15
Methods
Overview n In this presentation we will discuss these 4 topics: n n Main method vs. Jeroo methods Choosing behaviors to turn into methods Writing a Jeroo method Preconditions and postconditions
Jeroo has many built-in methods n Actions n n Sensor methods n n Hop, turn, … is. Net? is. Flower? … The programmer can define additional methods to extend the behavior of every Jeroo.
Behaviors and methods n n 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. 1 st define and name a behavior 2 nd write code for the method
Which parts of the program should be methods? n choose a behavior n n 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.
Writing the method n A Jeroo method contains the code describing what any Jeroo needs to do to carry out a particular behavior. Sub method_name ( ) • Choose a name that is meaningful Body of method • Empty parenthesis required on the header line (first line) Containing statements that describe how any arbitrary Jeroo can carry out this particular behavior. End Sub ‘== method_name== • Indent code inside the method
Important differences Main method n n Only one main method Must be called Main Always comes first Instantiate Jeroos Jeroo methods n n Can be many other methods Choose your own names Must be called by the main method Can’t instantiate in the method
Example of a Jeroo method Comments clearly explain what the method does ‘ ****************************** ‘ Turn Around: makes a Jeroo turn 180 degrees ‘ ****************************** Good , descriptive name for the method Sub turn. Around( ) turn(LEFT) End Sub The code is indented Notice that no Jeroo name is specified in the steps ‘ === end turn. Around === Everything in yellow is good!
Important difference Main method n All steps must specify which Jeroo does each action: objectname. method() Example in Main for a Jeroo named Betty n Betty. pick() n Betty. hop(2) n Betty. give(ahead) 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 n pick() n hop(2) n give(ahead) n
Using a Jeroo method n After you write your code Sub plant. Corner( ) plant() hop() turn(right) hop() plant() End Sub ‘ === plant Rectangle === n Use it just like any other method Sub main( ) Dim Ali as Jeroo = New Jeroo(5, 5, 8) Ali. plant. Corner( ) End Sub ‘ === main ===
One method can call another n The plant. Three method is used twice in the plant. Rectangle method e is t d o c os s i m h T sing s i m s ! of it ments com B A Sub plant. Three( ) plant( ) hop ( ) plant( ) End Sub ‘ === plant. Three === Sub plant. Rectangle( ) plant. Three( ) move. Down( ) plant. Three( ) End Sub ‘ === plant. Rectangle === Defined above Must also be defined somewhere
Preconditions and Postconditions n n A complete definition for a behavior includes: Preconditions n n Postconditions n n Assumptions of what is true before the method is called What will be true after the method is finished In Jeroo, the only things that change are: n n island cells (flower planted, net gone…) Jeroo attributes (location, number of flowers …)
Things to consider when writing a precondition n What must be true for this method to work? n Ask: does the Jeroo need n n 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 ‘ ********************** Sub plant. Three( ) …
Things to consider when writing a postcondition n Describe what is true after the method finishes n Ask: how will the method change n n 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 ‘ ********************** Sub plant. Three( ) …
The End