Adding A Method That Takes Parameter
Adding A Method That Takes Parameter
While some methods don’t need parameters, most do. Parameters allow a method to be generalized.
That is, a parameterized method can operate on a variety of data and/ or be used in a number of slightly
different situations. To illustrate this point, let’ a use a very simple example. Here is a method that
returns the square of 10;
int square()
retrun 10*10;
While this method does, indeed, return the value of 10 squared, its use is very limited. However, if we
modify the method so that it takes a parameter, as shown next, then we can make square() much more
useful.
return i*I;
Now square will return the square of whatever value it is called with. That is square() now a general
purpose method that can compute the square of any integer value, rather than just 10.
Here is an example
int x, y;
x=square(9); // x equals 81
y=2;
In the first call to square (), the value 5 will be passed into parameter i. In the second call, I will receive
the value 9. The third invocation passes the value of y, which is 2 in this example. As these example
shows, square() is able to return the square of whatever data it is passed
Parameter and Argument
A parameter is a variable defined by a method that receives a value when the method is called.
For example, square (100), passes 100 as an argument. Inside square (), the parameter i receives that
value.
We can use a parameterized method to improve the Box class. In the preceding example the
dimensions of each box had to be set separately by use of a sequence of statements, such as
mybox1.width=10;
mybox1.height=20;
mybox1.depth=15;
First, it is clumsy and error prone. For example, it would be easy to forget to set a dimension.
Second, in well-designed Java programs instance variables should be accessed only through methods
defined by their class.
In future we will change the behavior of a method, but we can not change the behavior of an exposed
instance variable.
Thus, a better approach to setting the dimensions of a box is to create a method that takes the
dimensions of a box in its parameters and sets each instance variable approximately. This concept can
be clear by following given program.
class Box {
double width;
double height;
double depth;
//compute and return volume
double volume()
{
return (width* height* depth);
}
//set dimensions of box
void setDim(double w, double h, double d)
{
width = w;
height= h;
depth= d;
}
}
class BoxDemo5{
public static void main(String args[])
{
Box mybox1= new Box();
Box mybox2= new Box();
double vol;
//initizalize each box
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
//get volume of first box
vol= mybox1.volume();
System.out.println("Volume is " +vol);
//get volume of second box
vol= mybox2.volume();
System.out.println("Volume is" +vol);
}
As we can see, the setDim() method is used to set the dimensions of each box. For example, when
is executed, 10 is copied into parameter w, 20 is copied into h. and 15 is copied into d. Inside setDim()
the values of w, h and d are then assigned to width, height and depth, respectively.