Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
2.3. Calling Object Methods Without Parameters¶
Methods define common behavior for all objects of the class.
Turtle class, methods
turnRight() give turtle objects the ability to
move forward 100 pixels and turn 90 degrees right.
The methods written for the Turtle class are called object methods or non-static methods. An object method must be called on an object of the class. Object methods work with the attributes of the object, such as the direction the turtle is heading or its position.
To call an object method, you must use a variable that references an object, along with the dot (.) operator followed
by the method name. For example,
yertle.forward(); calls the “forward” method on the turtle referenced
by the variable “yertle”, which results in a change to yertle’s location.
On the other hand,
myrtle.turnRight() calls the “turnRight” method on a different turtle object
referenced by the “myrtle” variable, resulting in a change to myrtle’s heading.
Every method call is followed by parentheses.
() after method names are there in case you need to give the
method parameters (data) to do its job.
You must always include the parentheses after the method name.
The following code uses a turtle to draw the digital number 7, but the lines are mixed up. Drag the code blocks to the right and put them in the correct order to first draw the line going up (towards the top of the page) and then turn and draw a line to the left to make a 7. Remember that the turtle is facing the top of the page when it is first created.
Can you make yertle draw the digital number 8, as 2 squares on top of each other?
2.3.1. Programming Challenge : Draw two letters¶
Write code in the editor window below (or use the repl.it link) to create 2 turtles to draw a pair of simple block-style letters that use just straight lines (no curves or diagonals). Each turtle should draw a different letter at a different location. Go back to the previous lesson and look over the various Turtle constructors if you forget how to create a turtle at a particular x,y location.
Here are some simple turtle methods that you can use:
You can make the world larger if you need more space to draw.
You may notice that it is challenging to have your turtles draw with these simple methods, which do not have formal parameters (there is nothing between the left and right parenthesis). In the next lesson, we will use more complex methods that include formal parameters, where you can indicate how many pixels the turtle should move or what angle to turn!
Methods are a set of instructions that define the behaviors for objects of the class.
Use dot notation to execute an object’s method. This is the object’s name followed by the dot (.) operator followed by the method name and parentheses: object.method();
A method signature is the method name followed by the parameter list which gives the type and name for each parameter.
If a method has no parameters, you still need the parentheses after the name when you call the method.
An object method or non-static method is one that must be called on an object of a class. It usually works with the object’s attributes.
A static method or class method method is one that doesn’t need to be called on an object of a class.