2.5 Calling a Non-void Method
Similar to how methods can take in values as parameters, methods can also return values.
Recall the addTen()
method from the previous section:
This method takes in a parameter, x
, and adds 10 to it. Lastly, the program prints the value to the console.
But what if you wanted to store the value of x + 10
? In the method above, x + 10
is stored into a local variable called xPlusTen
. However, this variable is lost when the method is finished – it cannot be accessed outside of the method.
Using a return statement will allow you to pass a value back out of the method. That return value can be stored into a variable for later use.
Here’s how to rewrite addTen()
to return a value instead of printing:
There are a few differences here.
First, the return statement on line 4 replaces the print statement and does not require parentheses. Second, take a look at the first line of the method: public int addTen(int x)
. Instead of void
, you now use int
. This tells the program that the addTen()
method will return a value that is an int
. This is called the return type of the method.
Non-void methods return a value that is the same type as the return type in the signature. To use the return value when calling a non-void method, it must be stored in a variable or used as part of an expression. Up to this point, you’ve only used the void
return type, which indicates that the method does not return anything. Because the addTen()
method will return an integer, you set the return type to int
.
When returning a value, it’s important to note that that method is not printing the value to the console, but rather sending back to the method call statement. This is similar to how passing in a value as an argument does not print the value to the console.
A return value by itself would not be very useful, given that it does not print to the console. Fortunately, you can store a method’s return value into a variable. Take a look at the following code:
First, you create a variable named num
and initialize it to 7. Then, you create another variable called tenAdded
. Notice that tenAdded
is not given a normal value. Instead, you are setting it equal to addTen(num)
. This means that the tenAdded
variable will hold the result of whatever the method call addTen(num)
returns. You know that addTen(num)
will return 17, so tenAdded
will be 17.
Return values work in many situations. For example, you can rewrite the add method from the previous section to return the sum instead of print it to the screen:
You can now call the method and store its return values
which would print 100 to the console.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Suppose you have a class called
Rollercoaster
. TheRollercoaster
class has a method calledgoingDown
, partially defined belowWhich of the following statements correctly stores the return value of
goingDown
when it is called on theRollercoaster
object calledferrisWheel
?
The LaundryService
class is completed for you, but you may want to look at it to make sure you understand how to use it.
Then write a program in the main
method that asks the user to enter the sales tax rate as a decimal, the number of pounds of clothes needed to be laundered, and the number of shirts needed to be dry cleaned.
Create a LaundryService
object to compute the cost of the laundered clothes and dry cleaned shirts. Use $2 for the cost of laundered clothes per pound. Use $3 for the cost per dry-cleaned shirt.
Print the total cost. Then use the method to compute the tax and print out the grand total including tax.
Here is an example of the output