2.5. Calling Methods that Return Values

Some methods return a value back that the program can use. In the previous lesson, you may have used some set methods with parameters to set the attributes of a turtle to different values, for example yertle.setColor(Color.red); or yertle.setWidth(50); 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 always return back the value of that instance variable, and the set methods modify the value.

Here are some examples of using get methods for the turtle object yertle. 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(). If a method has void as its return type, like most of the methods we have seen so far, that means that it does not return anything and it can be called on its own without using its return value. But methods with return types need to be called in an assignment statement or a print statement or something similar to use what it returns.

Turtle yertle = new Turtle(world);
int width = yertle.getWidth();
int height = yertle.getHeight();
System.out.println("Yertle's width is: " + width);
System.out.println("Yertle's height is: " + height);
System.out.println("Yertle's x position is: " + yertle.getXPos() );
System.out.println("Yertle's y position is: " + yertle.getYPos() );

Note

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.

coding exercise Coding Exercise:

Try the code below that changes the turtle’s width and height. How big or small can you make yertle?

(If the code below does not work for you, you can also copy in the code below into 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.)

Another common method that returns a value is the toString() method which returns a String description of the turtle. This method is called automatically to try to convert an object to a String when it is needed, for example in a print statement.

Turtle yertle = new Turtle(world);
yertle.setName("yertle"); // set name before you use toString()
System.out.println(yertle.toString());
// Or you can just use the object here and it will call toString() automatically!
System.out.println(yertle);

coding exercise Coding Exercise:

Try some of the get methods and the toString() method in the program below. Note that you have to print out what the get methods return in order to see what they do!

2.5.1. groupwork Programming Challenge : Turtle Distances

  1. The Turtle class has a method called getDistance(x,y) which will return the turtle’s distance from a point (x,y). Can you find yertle’s distance from the point (0,0)?

  2. Add another turtle and make both turtles move. Then find the distance between them. You must use the getXPos() and getYPos() methods as well as the getDistance() method.

2.5.2. 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.

You have attempted of activities on this page
Next Section - 2.6. Strings