DEV Community

Arun Kumar
Arun Kumar

Posted on

Parameters in java

๐Ÿค”I should learn from this topic in this web site
(https://math.hws.edu/javanotes/c4/s3.html)

๐Ÿ™„Parameters.

A subroutine is a black box, then a parameter is something that provides a mechanism for passing information from the outside world into the box. Parameters are part of the interface of a subroutine. They allow you to customize the behavior of a subroutine to adapt it to a particular situation.

As an example, let's go back to the "3N+1" problem that was discussed in Subsection. Suppose that we want to write a subroutine to print out such sequences. The subroutine will always perform the same task: Print out a 3N+1 sequence. But the exact sequence it prints out depends on the starting value of N. So, the starting value of N would be a parameter to the subroutine. The subroutine can be written like this

The parameter list of this subroutine, "(int startingValue)", specifies that the subroutine has one parameter, of type int. Within the body of the subroutine, the parameter name can be used in the same way as a variable name. But notice that there is nothing in the subroutine definition that gives a value to the parameter! The parameter gets its initial value from outside the subroutine. When the subroutine is called, a value must be provided for the parameter in the subroutine call statement. This value will be assigned to startingValue before the body of the subroutine is executed. For example, the subroutine could be called using the subroutine call statement "print3NSequence(17);". When the computer executes this statement, the computer first assigns the value 17 to startingValue and then executes the statements in the subroutine. This prints the 3N+1 sequence starting from 17. If K is a variable of type int, then the subroutine can be called by saying "print3NSequence(K);". When the computer executes this subroutine call statement, it takes the value of the variable K, assigns that value to startingValue, and then executes the body of the subroutine.

The class that contains print3NSequence can contain a main() routine (or other subroutines) that call print3NSequence. For example, here is a main() program that prints out 3N+1 sequences for various starting values specified by the user:
Remember that before you can use this program, the definitions of main and of print3NSequence must both be wrapped inside a class definition.

In references to this topics what's return in my knowledge trainer was given the task for limited calculator operations example arithmetic operations in i should learn from this topic

I will write a program for this calculator program in my life i write on this is biggest problem around 45 lines are i write a program
Get a output are loat of time to taken and finally write a wright program in i write program was attached bellow

๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘‰๐Ÿ‘‰
class Calculation {

public int add(int a, int b) {
    return a + b;
}
public int add(int a, int b, int c) {
    return a + b + c;
}
public int sub(int a, int b) {
return a - b;
}
public int sub(int a, int b, int c) {
    return a - b - c;
}
public int mul(int a, int b) {
  return a * b;
}
public int mul(int a, int b, int c) {
   return a * b * c;
}
public float div(float  a, float  b) {
float div1 = a / b;
return div1;

}

public float  div(float  a, float  b, float  c) {
float div2 = a / b / c;
    return div2;
}

public int mod(int a, int b) {

    return a % b;
}

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

    return a % b % c;
}

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

        int a = 200;
        int b = 400;
        int c = 700;

        System.out.println("Results of values:");
        System.out.println("Addition of two: " + calc.add(a, b));
        System.out.println("Addition of three: " + calc.add(a, b, c));
        System.out.println("Subtraction of two: " + calc.sub(a, b));
        System.out.println("Subtraction of three: " + calc.sub(a, b, c));
        System.out.println("Multiplication of two: " + calc.mul(a, b));
        System.out.println("Multiplication of three: " + calc.mul(a, b, c));
        System.out.println("Division of two: " + calc.div(a, b));
        System.out.println("Division of three: " + calc.div(a, b, c));
        System.out.println("Modulus of two: " + calc.mod(a, b));
        System.out.println("Modulus of three: " + calc.mod(a, b, c));

}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)