Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Parameters

Methods are a set of instructions that define behaviors for all objects of a class. For example, in a Turtle class, methods like forward() and turnRight() give Turtle objects the ability to move forward and turn 90 degrees right.

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 a turtle object forward 100 pixels. These are called object methods. An object method must be called on an object of the class that the method is defined in.

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, which we will see soon. You must always include the parentheses after the method name.

The parentheses () after method names are there in case you need to give the method arguments (some data) to do its job. For example, we can give the argument 100 in forward(100) to make the turtle go forward 100 pixels or the argument 30 in turn(30) to make the turtle turn 30 degrees instead of 90 degrees.

Arguments vs. Parameters

Although some people use the words parameters and arguments interchangeably, there is a subtle difference. When you create your own method, the variables you define for it are called formal parameters. When you call the method to do its job, you give or pass in arguments or actual parameters to it that are then saved in the parameter variables. So, in the definition of the forward method, it has a parameter variable called pixels, and in the call to forward(100), the argument is the value 100 which will get saved in the parameter variable pixels. You will learn to write your own methods in Unit 5. In this unit, you will learn to call methods that are already written for you.

Here is the Turtle class diagram again that shows some of the variables and methods inherited from the SimpleTurtle class in the class Turtle that are written for you.

Try some of the methods above in the turtle code below. You can see all the methods that are inherited in Turtle in this javadoc (documentation) file.

Programming Challenge:

  1. Can you draw a triangle? The turnRight() method always does 90 degree turns, but you’ll need external angles of 120 degrees for an equilateral triangle. Use the turn method which has a parameter for the angle of the turn in degrees. For example, turn(90) is the same as turnRight().
  2. Once you can draw a triangle, can you draw a house with windows? You may make it with different colors by using the Java Colors class.
yertle.setColor(Color.RED);

To draw a window, you will need to do penUp() to walk the turtle into position, for example:

builder.penUp();
builder.moveTo(120,200);
builder.penDown();