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 parameters, 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.

(If the code below does not work for you, you can also use the Turtle code at this repl.it link (refresh page after forking and if it gets stuck) or download the files here to use in your own IDE.)

Procedural abstraction allows a programmer to use a method and abstract away the details of how it exactly works. For example, we know that if we hit the brakes, the car will stop, and we can still use the brakes even if we don’t really know how they work.

When we use methods for a class in a library, we can look up the method signature in its documentation to see its name and parameters (which can be an empty list for methods without parameters). When we call a method or a constructor, this makes the program go do all the code in that method before continuing on with the next sequential line of code. Once the last statement in the method has executed, flow of control is returned to the point immediately following where the method was called.

coding exercise Coding Exercise:

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

Before you call a method, you must make sure that you have created and intialized an object using a constructor. Remember that if you just declare a Turtle object without calling its constructor, it will be null and not really reference an object. If you call a method for a variable that is null, you will get an error called NullPointerException.

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