Today Java Class :

Today Java Class :

Publish Date: Aug 27
5 0

1) Parameter Method in Java

*In Java, a "parameter method" refers to a method that is defined to accept one or more input values, known as parameters. These parameters are specified within the parentheses of the method's declaration and act as placeholders for the actual values (arguments) that will be passed to the method when it is called.

public class Calculator {
public static void add(int num1, int num2) {
int result = num1 + num2;
System.out.println("The sum is: " + result);
}

public static void main(String[] args) {
    add(5, 7); // Output: The sum is: 12
}
Enter fullscreen mode Exit fullscreen mode

}

2) In this example:

*num1 and num2 are parameters of the add method.

*When calling the add method, we pass 5 and 7 as arguments, which are assigned to num1 and num2, respectively.

3) Types of Parameters:

  1. Formal Parameters: These are the

parameters defined in the method signature (e.g., num1 and num2).

  1. Actual Parameters (or Arguments): These are the values passed to the method when it's called (e.g., 5 and 7).

4) Key Points:

Parameters can be of any data type (primitive or reference).

Methods can have multiple parameters, separated by commas.

Parameters can be used within the method to perform operations.

My Task Java Program In Parameters :

class Calculator{
int a;
int b;
int c;

public int add(int a,int b,int c){
int d = a+b+c;
return d;
}

public int add (int a,int b){
int c = a+b;
return c;
}

public int sub (int a, int b, int c){

int d = a-b-c;
return d;
}

public int sub (int a, int b){
int c = a-b;
return c;
}

public int multi (int a, int b, int c){
int d = a*b*c;
return d;
}

public int multi (int a, int b){
int c = a*b;
return c;
}

public int divi (int a, int b, int c){
int d = a/b/c;
return d;
}

public int divi (int a, int b){
int c = a/b;
return c;
Enter fullscreen mode Exit fullscreen mode

}

public int modul (int a, int b, int c){
int d = a%b%c;
return d;
Enter fullscreen mode Exit fullscreen mode

}

public int modul (int a, int b){
int c = a%b;
return c;
}

public static void main(String[] args) {
Calculator calc = new Calculator();

    System.out.println("Addition (2 nums): " + calc.add(10, 20));
    System.out.println("Addition (3 nums): " + calc.add(10, 20, 30));

    System.out.println("Subtraction (2 nums): " + calc.sub(20, 50));
    System.out.println("Subtraction (3 nums): " + calc.sub(100, 40, 10));

    System.out.println("Multiplication (2 nums): " + calc.multi(15, 5));
    System.out.println("Multiplication (3 nums): " + calc.multi(4, 7, 9));

    System.out.println("Division (2 nums): " + calc.divi(20, 7));
    System.out.println("Division (3 nums): " + calc.divi(110, 5, 2));

    System.out.println("Modulus (2 nums): " + calc.modul(20, 3));
    System.out.println("Modulus (3 nums): " + calc.modul(70, 7, 17));
}
Enter fullscreen mode Exit fullscreen mode

}

OUTPUT :

Addition (2 nums): 30
Addition (3 nums): 60
Subtraction (2 nums): -30
Subtraction (3 nums): 50
Multiplication (2 nums): 75
Multiplication (3 nums): 252
Division (2 nums): 2
Division (3 nums): 11
Modulus (2 nums): 2
Modulus (3 nums): 0

Comments 0 total

    Add comment