
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass Method Call as Arguments to Another Method in Java
In this article, we will understand how to pass method call as arguments to another method. We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter two numbers : 2 and 3
Output
The desired output would be −
The cube of the sum of two numbers is: 125
Algorithm
Step 1 - START Step 2 - Declare two variables values namely my_input_1 and my_input_2 Step 3 - We define a function that takes two numbers, and returns their sum. Step 4 - We define another function that takes one argument and multiplies it thrice, and returns the output. Step 5 - In the main function, we create a new object of the class, and create a Scanner object. Step 6 - Now, we can either pre-define the number or prompt the user to enter it. Step 7 - Once we have the inputs in place, we invoke the function that returns the cube of the input. Step 8 - This result is displayed on the console.
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class Main { public int my_sum(int a, int b) { int sum = a + b; return sum; } public void my_cube(int my_input) { int my_result = my_input * my_input * my_input; System.out.println(my_result); } public static void main(String[] args) { Main obj = new Main(); int my_input_1, my_input_2; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the first number : "); my_input_1 = my_scanner.nextInt(); System.out.print("Enter the second number : "); my_input_2 = my_scanner.nextInt(); System.out.println("The cube of the sum of two numbers is: "); obj.my_cube(obj.my_sum(my_input_1, my_input_2)); } }
Output
Required packages have been imported A reader object has been defined Enter the first number : 2 Enter the second number : 3 The cube of the sum of two numbers is: 125
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class Main { public int my_sum(int a, int b) { int sum = a + b; return sum; } public void my_cube(int my_input) { int my_result = my_input * my_input * my_input; System.out.println(my_result); } public static void main(String[] args) { Main obj = new Main(); int my_input_1, my_input_2; my_input_1 = 3; my_input_2 = 2; System.out.println("The two number is defined as " +my_input_1 +" and " +my_input_2); System.out.println("The cube of the sum of two numbers is: "); obj.my_cube(obj.my_sum(my_input_1, my_input_2)); } }
Output
The two number is defined as 3 and 2 The cube of the sum of two numbers is: 125
Advertisements