create and call non-void methods with parameters and return values
If a method is a void method and has void as its return type, like most of the methods we have seen so far, that means that it does not return anything.
In the previous lesson, we saw some set methods with parameters to set the attributes of a turtle to different values, for example t.setColor(Color.red); or t.setWidth(50);
But some methods, known as non-void methods, return a value back that the program can use.
GET METHODS
Get methods return the value of instance variables, for example getWidth() and getHeight() to get the height and width for a Turtle object.
Programmers create get and set methods for each attribute represented as an instance variable in a class to access and modify the value in that variable.
The get methods (aka accessor methods) always return back the value of that instance variable, and the set methods (aka mutator methods) modify the value.
When you use a get method, you need to save what it returns in a variable or use the value in some way for example by printing it out.
The data type of the variable must match the data type of the return value of the method.
You can find out the return type of a method in its documentation.
It will be right before the method name, for example int getWidth()means getWidth will return an int (an integer number).
Here are some examples of using get methods for the turtle object t.
Turtle t = new Turtle(world);
int width = t.getWidth();
int height = t.getHeight();
System.out.println("T's width is: " + width);
System.out.println("T's height is: " + height);
System.out.println("T's x position is: " + t.getXPos() );
System.out.println("T's y position is: " + t.getYPos() );
A common error is forgetting to do something with the value returned from a method.
When you call a method that returns a value, you should do something with that value like saving it into a variable or printing it out.
TOSTRING() METHOD
Another common method that returns a value is the toString() method.
This method is called automatically to try to convert an object to a String when it is needed, for example in a print statement.
In the Turtle class, the toString() method returns a String description of the turtle.
Turtle t = new Turtle(world);
t.setName("Dave"); // set name before you use toString()
System.out.println(t.toString());
// Or you can just use the object here and it will call toString() automatically!
System.out.println(t);
METHODS WITH ARGUMENTS AND RETURN VALUES
Methods that take arguments and return values are like mathematical functions. Given some input, they return a value.
For example, a square(x) method would take an argument x and return its square by multiplying it by itself.
You will not write your own methods until Unit 5, but you should be able to trace through method calls.
The return statement in a method returns the value that is indicated in the return type back to the calling method.
The calling method must save or use or print that value.
SUMMARY
Some methods return values.
To use the return value when calling a method, it must be stored in a variable or used as part of an expression. The variable data type must match the return type of the method.
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.