2.3. Calling Object Methods Without Parameters

Methods define common behavior for all objects of the class. In the Turtle class, methods like forward() and 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. The 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.

exercise Check Your Understanding: Mixed-up Code

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.

        public class Draw7
{
---
    public static void main(String[] args)
    {
---
        World world = new World(300,300);
---
        Turtle yertle = new Turtle(world);
---
        yertle.forward();
---
        yertle.turnLeft();
        yertle.forward();
---
        world.show(true);
---
    } // end main
---
} // end class
        

coding exercise Coding Exercise:

Can you make yertle draw the digital number 8, as 2 squares on top of each other?

2.3.1. groupwork 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:

  • forward();

  • turnLeft();

  • turnRight();

  • backward();

  • penUp();

  • penDown();

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!

2.3.2. Summary

  • 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.

You have attempted of activities on this page