Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Java

जावा प्रोग्राम विधि कॉल को किसी अन्य विधि के तर्क के रूप में पास करने के लिए

इस लेख में, हम समझेंगे कि विधि कॉल को किसी अन्य विधि के तर्क के रूप में कैसे पास किया जाए। हम किसी अन्य वर्ग के अंदर उस वर्ग का एक ऑब्जेक्ट बनाकर किसी अन्य वर्ग से एक विधि को कॉल कर सकते हैं। ऑब्जेक्ट बनाने के बाद, ऑब्जेक्ट रेफरेंस वेरिएबल का उपयोग करके विधियों को कॉल करें।

नीचे उसी का एक प्रदर्शन है -

इनपुट

मान लीजिए हमारा इनपुट है -

Enter two numbers : 2 and 3

आउटपुट

वांछित आउटपुट होगा -

The cube of the sum of two numbers is:
125

एल्गोरिदम

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.

उदाहरण 1

यहां, उपयोगकर्ता द्वारा एक संकेत के आधार पर इनपुट दर्ज किया जा रहा है। आप इस उदाहरण को हमारे कोडिंग ग्राउंड टूल में लाइव देख सकते हैं जावा प्रोग्राम विधि कॉल को किसी अन्य विधि के तर्क के रूप में पास करने के लिए

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));
   }
}

आउटपुट

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

उदाहरण 2

यहां, पूर्णांक को पहले परिभाषित किया गया है, और इसके मान को एक्सेस किया जाता है और कंसोल पर प्रदर्शित किया जाता है।

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));
   }
}

आउटपुट

The two number is defined as 3 and 2
The cube of the sum of two numbers is:
125

  1. जावास्क्रिप्ट कॉल () तर्क के साथ विधि।

    जावास्क्रिप्ट कॉल () फ़ंक्शन हमें विभिन्न वस्तुओं से एक ही विधि का उपयोग करने की अनुमति देता है। यहां पैरामीटर अलग से पास किए गए हैं। जावास्क्रिप्ट फ़ंक्शन कॉल के लिए कोड निम्नलिखित है () - उदाहरण दस्तावेज़ बॉडी { फॉन्ट-फ़ैमिली:सेगो यूआई, ताहोमा, जिनेवा, वर्दाना, सेन्स-सेरिफ़; } .नमूना { फ़ॉन्ट-आका

  1. हम जावा में इनवोकलेटर () विधि को कैसे कॉल कर सकते हैं?

    एक आह्वानबाद में() विधि एक स्थिर . है स्विंग यूटिलिटीज . की विधि वर्ग और इसका उपयोग किसी कार्य को करने के लिए किया जा सकता है अतुल्यकालिक रूप से एडब्ल्यूटी . में ईवेंट डिस्पैचर थ्रेड . SwingUtilities.invokeLater() विधि SwingUtilities.invokeAndWait() . की तरह काम करती है सिवाय इसके कि यह अनुरोध

  1. जावा में विधि ओवरलोडिंग

    मेथड ओवरलोडिंग एक प्रकार का स्टैटिक पॉलीमॉर्फिज्म है। मेथड ओवरलोडिंग में, हम एक ही नाम के साथ लेकिन विभिन्न मापदंडों के साथ कई विधियों को परिभाषित कर सकते हैं। निम्नलिखित उदाहरण कार्यक्रम पर विचार करें। उदाहरण public class Tester {    public static void main(String args[]) {