কম্পিউটার

জাভা প্রোগ্রাম অন্য পদ্ধতিতে আর্গুমেন্ট হিসাবে মেথড কল পাস করতে


এই নিবন্ধে, আমরা বুঝব কিভাবে অন্য পদ্ধতিতে আর্গুমেন্ট হিসাবে মেথড কল পাস করতে হয়। আমরা অন্য ক্লাস থেকে একটি মেথড কল করতে পারি শুধুমাত্র অন্য ক্লাসের ভিতরে সেই ক্লাসের একটি অবজেক্ট তৈরি করে। একটি অবজেক্ট তৈরি করার পর, অবজেক্ট রেফারেন্স ভেরিয়েবল ব্যবহার করে পদ্ধতি কল করুন।

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ইনপুট

ধরুন আমাদের ইনপুট হল −

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. আর্গুমেন্ট সহ জাভাস্ক্রিপ্ট কল() পদ্ধতি।

  2. কিভাবে আমরা জাভাতে invokeLater() পদ্ধতি কল করতে পারি?

  3. জাভাতে একটি পদ্ধতি ওভারলোড করার বিভিন্ন উপায়

  4. জাভাতে ওভারলোডিং পদ্ধতি