0% found this document useful (0 votes)
29 views3 pages

Adding A Method That Takes Parameter

This document discusses adding parameters to methods to make them more generalized and reusable. It provides an example of modifying a square() method to take an integer parameter rather than just returning 10 squared. This allows square() to calculate the square of any integer passed to it. The document also defines the difference between a parameter, which is a variable that receives a value in a method, and an argument, which is the actual value passed to the method. Finally, it shows how to improve a Box class by adding a setDim() method that takes width, height, and depth parameters to initialize each box's dimensions rather than setting them individually.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Adding A Method That Takes Parameter

This document discusses adding parameters to methods to make them more generalized and reusable. It provides an example of modifying a square() method to take an integer parameter rather than just returning 10 squared. This allows square() to calculate the square of any integer passed to it. The document also defines the difference between a parameter, which is a variable that receives a value in a method, and an argument, which is the actual value passed to the method. Finally, it shows how to improve a Box class by adding a setDim() method that takes width, height, and depth parameters to initialize each box's dimensions rather than setting them individually.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

int square (int i)

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 (5); //x equals 25

x=square(9); // x equals 81

y=2;

x=square(y); //x equals 4

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 in square (i), i is a parameter.

An argument is a value that is passed to a method when it is invoked.

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;

While this code works, it troubling for two reasons

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.

//this program uses a parameterized method

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

mybox1.setDim(10, 20, 15);

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.

You might also like