πŸ““2.5: Methods that Return Values

Table of Contents


πŸ“– This page is a condensed version of CSAwesome Topic 2.5

πŸ“ Take notes in a Codespace during class, coding along with the instructor.

  1. Go to GitHub and click on your picture in the TOP RIGHT corner
  2. Select Your repositories
  3. Open CS2-Unit-2-Notes
  4. Now on your repository, click and select the Codespaces tab
  5. Click Create Codespace on main (unless you already have one listed there), wait for the environment to load, then you’re ready to code!

Methods that Return Values

All the methods on Turtle that we’ve discussed so far have had a void return type. Such methods are sometimes called void methods.

⬛️ Because a void method does NOT return any value, the only point of calling one is because it does something that can be observed by the user or by other codeβ€”it changes the state of the object or maybe causes something to happen like drawing a line on the screen. Or both.

πŸͺ„ These things they do are sometimes called effects.

Methods with a return type of anything other than void are called non-void methods. These methods return a value (output) that the code calling the method can use.

And because methods are called on an object, these methods can be used to return values that tell us things about an object’s internal state.

Most methods are of one kind or the other: either a void method which is called for some effect or a non-void method that is called to compute a value but otherwise has no effect.

To put it another way, void methods do things while non-void methods produce values.

πŸ–¨οΈ The Scanner Class

The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object instance of the class and use any of the available methods found in the Scanner class documentation. More info on the Scanner class

Using Scanner Objects for Input

  1. Add an import statement at the very top of your program, BEFORE the class declaration:
    import java.util.Scanner; // import Scanner class
    
  2. Construct a Scanner object:
    Scanner scan = new Scanner(System.in); 
    
  3. You only need to construct the Scanner once in the program. Now, you can call its methods to take different types of user input:
    System.out.println("Enter some text: ");
    String strInput = scan.nextLine();
    System.out.println("Enter a whole number: ");
    String intInput = scan.nextInt();
    System.out.println("Enter a decimal number: ");
    String doubleInput = scan.nextDouble();
    
  4. Because the Scanner methods RETURN a value (the user’s input), you need to do something with it:
    // Print out the return value
    System.out.println("You entered " + strInput);
    // Use the return value in an expression
    int sum = intInput + doubleInput;
    

πŸ’‘ The Scanner class is NOT TESTED on the AP Exam. However, it is very useful in our programs because it enables user interaction!

Accessor / β€œGetter” Methods

A simple kind of method that returns a value is what is formally called an accessor because it accesses a value in an object.

  • In the real world everyone calls them getters.
  • A getter is a method that takes no arguments and has a non-void return type.
  • In Java they are almost always named something that starts with get, and they usually just return the value of one of the object’s instance variables.

    For example, the Turtle class has several getters, getWidth and getHeight which return the width and the height of a Turtle object and getXPos and getYPos which return the x and y values of the Turtle’s position.

Here are some examples of using getters on the Turtle object yertle.

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() );

⚠️ A common mistake 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 output, like assigning it to a variable, using it in an expression, or printing it out.

Methods with Arguments and a Return Value

Since getters take no arguments, all they can do is return a value based on the current state of the object. But often it’s useful to have methods that compute values based on both the current state of the object and some arguments.

For example, while we could use a Turtle’s getXPos and getYPos getters and some math to figure out how far away a Turtle is from any given point, if that’s a thing we need to do in a lot of programs using Turtle, it might be nice to be able to ask a Turtle directly for its distance from a given point.

And indeed, the Turtle class has a method called getDistance that takes two int arguments representing an x value and a y value and returns the distance between the Turtle’s current position and that x,y point. This is not a getter because it doesn’t just get an existing value; it computes a new value based on the arguments it is passed as well as the state of the Turtle.

Methods that take arguments and return values are somewhat like mathematical functions. Given some input, they return output.

(Mathematicians expect that a function always returns the same value, given the same arguments. So they would not consider something like getDistance(x, y) a true function since its return value also depends on the current position of the Turtle. But we’re doing programming, not math.)

image

πŸ’» In-Class Activity

  1. Go to
  2. Make sure you SIGN IN!
  3. Complete the Programming Challenge: Turtle Distances activity in pairs.

⭐️ Summary

  • Non-void methods are methods that return values (provide output).

  • Non-void methods typically do not have other effects, and are called purely to generate the value they return.

  • It is up to the caller of a non-void method to do something with the return value, such as assigning it to a variable or using it as part of an expression.

  • The value returned by a method has to match the declared return type of the method. Thus it can only be used where a value of that type is allowed, such as being assigned to a variable of that type.


πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK!

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Click the button on the LEFT menu
  3. Type a brief commit message at the top of the file that opens, for example: updated Main.java
  4. Click the small βœ”οΈ checkmark in the TOP RIGHT corner
  5. Click the button on the LEFT menu
  6. Finally you can close your Codespace!

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.