Java | Parameters

05 min read

Methods taking Parameters:

Parameter: A variable defined by a method that receives a value when the method is called. (eg) in 'int square(int i)', 'i' is a parameter.

Argument: The value passed to a method when it is invoked. (eg) square(100) passes 100 as an argument.

The getdata( ) method can be modified in such a way that it takes parameters. The input values can be read in the main() method and passed to getdata( ).

class Computer
{
.....
public void getdata(int r, int h, float f, String s)
{
RAM = r;
hdisk = h;
freq = f;
make = s;
}
.....
}
classComputerMain
{
public static void main(String args[ ])
{
Computer node1= new Computer( );
Computer node2 = new Computer( );
nodel.getdata(1, 200, 1.2e06, "Intel"); ---> The values received by the parameters in the getdata( ) method & eventually assigned to the instance variables.
Scanner sc = new Scanner(System.in);
int r = sc.nextInt( );                                                  
int h = sc.nextInt( );                                             
float f = sc.nextFloat( );
String s = sc.next( );
node2.getdata(r, h, f, s);
node1.display( );
node2.display( );
}
}
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error