2.3. Calling Methods Without Parameters

Methods define the behaviors or functions for objects. For example, in the Turtle class, methods like forward() and turnRight() made the turtle objects move that way.

To use an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example, yertle.forward(); calls yertle’s forward method to move the turtle forward. Each method call is followed by parentheses. The parentheses () after method names are there in case you need to give the method some data to do its job, which we will see in the next lesson. You must always put in the parentheses after the method name.

Note

object.method(); is used to call an object’s method.

exercise Check Your Understanding

Try to put the following mixed up code in the right order to draw the digital number 7 with just a line going straight up and then a line going left.

        2-3-1: 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 and then the line going across to the left to make a 7. Remember that the turtle is facing up at the start. Click on the "Check Me" button to check your solution.  You can copy and paste this code in the Active Code window below to see it in action.public class DrawL
{
---
   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);
      ---
   }
   ---
}
        

coding exercise Coding Exercise:

After you put the mixed up code in order above, type in the same code below to make the turtle draw a 7 with just a line up and then across.

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 a Letter

Working in pairs, use the area below to have a turtle draw a simple letter or number that uses just straight lines (no curves or diagonals). It could be an initial in your name or a number from today’s date.

It may help to act out the code pretending you are the turtle. Remember that which way you turn depends on which direction you are facing, and the turtle begins facing up.

Here are some simple turtle methods that you could use:

  • yertle.forward();

  • yertle.turnLeft();

  • myrtle.turnRight();

  • myrtle.backward();

  • myrtle.penUp();

  • yertle.penDown();

You may notice that it is challenging to have your turtle draw with these simple methods. In the next lesson, we will use more complex Turtle methods where you can indicate how many steps to take or what angle to turn that will make drawing a lot easier!

After writing your code below, if you’d like your own copy, you can open this repl.it link, copy in your code, and save it in your own repl.it account.

2.3.2. Summary

  • Methods define the behaviors or functions for objects.

  • To use an object’s method, you must use the object name and the dot (.) operator followed by the method name, for example object.method();

You have attempted of activities on this page
Next Section - 2.4. Calling Methods With Parameters