Google+

73. How does a method intake parameters ?






Parameters allow a method to be generalized. That is, a parameterized method can operate on a variety of data.

Example of a method without parameters:

int square( )
{
     return 10*10;
}

While this method does return the value of 10 squared, its use is very limited. However, if you modify this method so that it takes a parameter, then you can make square( ) method much more general and useful.

Example of a method with parameters:

int square(int i)
{
     return i*i;
}

Now square( ) method will return the square of whatever value it is called with. That is, square( ) method is now a general purpose method that can compute the square value of any given integer value, rather than just 10. In the below example we will pass the values to the parameter i in the above mentioned method square(int i), by calling it from a method in a different class using the below:

object.square(5);  -> Number 5 will be passed to the parameter i in the square(int i)  method of 'Square' Class.

Lets implement this in Eclipse IDE:

1. Create a new Java project say 'Fourth Project' as shown below:



2. Create a package say 'fourth_package' under 'Fourth Project' project as shown below:


3. Right click on the 'fourth_package' package and select



4. In 'New Java Class' dialog, enter class name as 'Square' and deselect all the options marked in the below screenshot (i.e. Create Class without main( ) method) -


5. Ensure that the Class 'Square' is created and displayed as shown  below -



6. Enter the following code into the 'square' class as shown below and save:



7. Now create another class 'PassingValues' with main( ) method as shown below:



8. Create an object s1 of 'Square' class in 'PassingValues' class as shown below:



9. Declaring an integer variable to store the values returned by square( ) method in Square class as shown below:



10. Call the square( ) method in Square class by passing values as shown below and print the values returned:



11. Save the 'Passing Values' class and Run it as Java Application as shown below:


12. Observe the output is displayed in the console as shown below:



After going through this program, we've to understand the following two terms -
  • parameter - A parameter is a variable defined by a method that receives a value when the method is called (i.e. square(int i) -> 'i' is the parameter, that receives the values when the square( ) method in Square class is called from 'PassingValues' class )
  • argument  - An argument is  a value that is passed to a method when it is invoked (i.e. s1.square(5) ->   5 is the argument that is passed by 'Passing Values' class to square(int i) method in Square class )
Download this project:

Click here to download the project containing the 'Square' and 'Passing Values' class files. (You can import the downloaded project into Eclipse IDE installed on your machine)

Before going to the next program, we've to understand the following:

In the previous examples of  Box,  the dimensions of each method has to be set separately by use of sequence of statements, such as:

box1.width = 10;
box1.height = 10;
box1.depth = 10;

This method of setting the dimension for the variables of Box class from the other class is not recommended because of the following reason.
  • In Java programs, instance variables should be accessed only through methods defined by their class (But in our previous examples of Box, we've accessed the instance variables of Box class from other class by using statements like box1.width = 10; etc )
So, 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 variables of a class appropriately.

Lets implement this in Eclipse IDE:

1. Launch Eclipse IDE, ensure that 'Third Project' java project is available, remove the existing code in Box Class and Define instance variables height, width and depth in Box class as shown below:



2. Create a method for setting the dimensions of the box (i..e. receives the arguments from other class and assigns them to the instance variables created in Box class i.e. width, height and depth) as shown below:



Since this method 'DimesionsSet' is not required to return anything, observe that we've specified its type as void before the method name.

3. Creating another method to calculate the volume of the box and return the result as shown below and save the class:



4. Create another class 'ArgumentsPassing'  under the same package, the Box class is created as shown below:



5. Create two objects box1 and box2 as instances of Box class as shown below:



6. Pass box1 and box2 dimensions as arguments to DimensionsSet( ) method in Box class as shown below:



7. Call the volume( ) methods from the print statements of box1 and box2 as shown below:



8. Save and Run the 'ArgumentsPassing' class as Java Application as shown below:


9. Observe that the output of the program is displayed in the console as shown below:



Download this Project:

Click here to download the project along with the class files used. (You can download this project and import into Eclipse IDE installed in your machine)




Please comment below to feedback or ask questions.

'Constructors' concept will be explained in the next post



5 comments:

raghavi said...

Hi arun

i have a question
in void method u assigned variables
width=w;
height=h;
depth=d;
instead of using double method
can we give like this? of course it wont return the value

System.out.println("volume is "+width*height*depth);

are we supposed to use this?


raghavi said...

package second_package;

public class Box {

double width;
double height;
double depth;

public void dimension(double w, double h, double d)
{
width = w;
height = h;
depth = d;
System.out.println(w*h*d);
}
//public double volume()
//{
//return width*height*depth;
// }

}

Argument passing:

package second_package;

public class Argumentpassing {

public static void main(String[] args) {
// TODO Auto-generated method stub
Box box1=new Box();
Box box2=new Box();

//box1.dimension(10, 20, 30);
//box2.dimension(3, 4, 5);
System.out.println(box1.dimension(5, 7, 8));
System.out.println(box2.dimension(2, 9, 9));


}

}

Am getting error as Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

at second_package.Argumentpassing.main(Argumentpassing.java:12)

Ranjith Kumar said...

Raghavi:

Remove the S.O.P in the below code and it will work.

Your Code:
System.out.println(box1.dimension(5, 7, 8));
System.out.println(box2.dimension(2, 9, 9));

Make it as:
box1.dimension(5, 7, 8);
box2.dimension(2, 9, 9);

Arun Motoori said...

@ Raghavi - (reply for first comment) We can use both ways based upon our requirement. Req#1 -> If your requirement is not to return any values to the calling method, you can directly print the value of volume in the called method.
Req#2 -> If your requirement is to use one method to set the dimensions of a box and have few other methods to calculate Volume and Area of the box. Then we have to create two other methods. One method for calculating the Volume as explained in my post and another method for calculating the Area.

(reply for second comment) You cannot print methods when they dont return any values.

In your comment, System.out.println(box1.dimension(5, 7, 8)); is trying to print the box1.dimension(5,7,8). This called method dimesion dont have anything to return. Hence the error.

If the called method is returning any value say int or double, then only you can use the method in the System.out.println() statement to print the returned value.

Hope it cleared your doubts :)

Unknown said...

Thanks Arun for this wonderful blog!! :)

I am really glad to find such a informative Blog.

I am a manual tester and only and only trying to learn selenium from ur blog and it is really helping me a lot :)
Thanks again...

@Ragavi as per the comment mentioned by Arun i have modified your code as below:Plz try it out

public double dimension(double w, double h, double d)
{
width = w;
height = h;
depth = d;
return(w*h*d);
}