UO.3 & UO.4: CALLING VOID METHODS WITH AND WITHOUT PARAMETERS
OBJECTIVES
I can:
create and call class methods
call non-static void methods with and without parameters
Methods are a set of instructions that define behaviors for all objects of a class.
For example, in the 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:
object.method();
We could call t's forward method to move the turtle object forward 100 pixels:
t.forward();
These are called object methods or non-static methods.
An object method MUST be called on an object of the class that the method is defined in.
In other words, world1.forward(); wouldn't work...
Methods work with the attributes of the object, such as the direction the turtle is heading or its position.
Every method call is followed by a set of parentheses ().
The parentheses after the 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 even if you DON'T have any parameters (data) that you wish to include.
PROCEDURAL ABSTRACTION
Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works.
For example, we know that if we hit the brakes, the car will slow down, and we can still use the brakes even if we don't really know how they work.
For now, we will focus on using methods already written for us and figure out what they do.
When we use the methods for a class in a library, we can look up the method signature (or method header), which is the method name followed by a parameter list, in its documentation.
Methods are defined after the instance variables (attributes) and constructors in a class.
For example, here is a Student class with a method signature public void print() which has an empty parameter list with no parameters:
Methods inside the same class can call each other using just methodName(), but to call non-static methods in another class or from a main method, you must first create an object of that class and then call its method using object.methodName():
Before you call a method from main or from outside of the current class, you must make sure that you have created and initialized an object.
Remember that if you just declare an object reference without setting it to refer to a new object the value will be null meaning that it doesn't reference an object.
If you call a method on a variable whose value is null, you will get a NullPointerException error, where pointer is another name for a reference.
CALLING METHODS WITH PARAMETERS
In the last lessons, we used simple methods like forward() and turnRight() to make the turtle draw lines.
You may have noticed that forward() and backward() always move the same number of pixels (100 pixels), and turnRight() and turnLeft() always turn at right angles (90 degrees).
This is a little limiting.
What if we wanted to draw a triangle or the letter A?
These require smaller angles to draw diagonal lines and different length lines.
Luckily, there are more complex methods in the Turtle class that let you specify the number of pixels to move forward or the number of degrees to turn.
These values that you can give to methods to help them do their job are called arguments or parameters.
The parentheses () after method names are there in case you need to give the method actual parameters or 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.
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.
Methods are said to be overloaded when there are multiple methods with the same name but a different method signature, where it requires a different number or type of parameters.
For example, we have two different forward methods, forward() with no parameters and forward(100) which has a parameter that tells it how much to move forward.
If there is more than one parameter, then the values given to the method need to correspond to the order and types in the method signature.
You will not write your own methods until Unit 5, but you should be able to trace and interpret method calls.
SUMMARY
Methods are a set of instructions that define the behaviors for all 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. Note that methods do not have to take any parameters, but you still need the parentheses after the method name.
Procedural abstraction allows a programmer to use a method by knowing in general what it does without knowing what lines of code execute. This is how we can drive a car without knowing exactly how the transmission or brakes work.
A method or constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing. Once the last statement in the method or constructor has been executed or a return statement is executed, the flow of control is returned to the point immediately following the method or constructor call.
A NullPointerException will happen if you try to call an object method on an object variable whose value is null. This usually means that you forgot to create the object using the new operator followed by the class name and parentheses.
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 is one that doesn't need to be called on an object of a class.
Methods define the behaviors or functions for objects.
Some methods take parameters/arguments that are placed inside the parentheses object.method(arguments).
Values provided in the parameter list need to correspond to the order and type in the method signature.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.