Returning a Value
While the implementation of volume( ) does move the computation of a box’s volume inside the Box class where it it is not the best way’ to do it. For example, what if another part of your program wanted to know the volume of a box, but not display its value? A better way to implement volume( ) is to have it compute the volume of the box and return the result fa the caller, The following example/an improved version of the preceding program, does just that.
executes, the ‘value of my box l.volume() is 3,000 and this value then is stored in vol. There are two important things to understand about returning values:
• The type of data returned by a meth~d must be compatible with the return type specified.by the method. For example, if the return type of some method is boolean, you could not return an integer.
• The variable receiving the value returned by a method (such as vol, in this case) must also be compatible with the return type specified ,for the method, One more point: The preceding program can be written a bit more efficiently , because there is actually no need for the vol variable. The call to volume t ) could have been used in the print lnf ) statement directly, as shown here.
Adding a Method That Takes, Parameters
‘While some methods don’t need parameters, most do. Parameters allow a method to be generalized. That is, a parameter method can operate on a variety of data and or be used in a number of slightly different situations. To illustrate this point, let’s use a very simple example. Here is a method that returns the square of the number.
In the first Call to square the value 5 will be passed into parameter i. In the second ‘ ‘call; 1.will receive the value 9. The third invocation passes the value, of y, which is 2 in ‘ this example. As these examples show, square( ) is able’ to return the square of whatever. data it is passed. It is important to keep the two terms parameter and argument straight. A parameter is , variable defamed by a method that receives a value when the method is called. For , example, in square( ), i is a parameter. An argument is a value that is passed to a method when it is invoked. For example, square(l00) passes 100 as an argument. .Inside square( ), the parameter receives that value. You can ,use a parameter method to improve the Box class. In the preceding ‘example the dimensions of each box had to beset separately by use of a sequence of .is executed, ,10is copied into parameter w, 20 is copied into h, and is is copied into d. Inside se.tDim( ) the values of w, h, and d are then assigned to width, height, and depth, respectively. For many readers=-especlally those experienced with C/C++-the concepts presented in the preceding sections will be familiar. However, if such things as method calls, arguments, and parameters are new to you, then you might want to take some· time to experiment before moving on. The concepts of the method invocation, parameters, and return values are fundamental to Java programming.