modifier TOPIC 3: JAVA LANGUAGE
3.5 METHOD
(METHOD DEFINITION and METHOD CALL for type 3 and Type 4)
Method Definition Method Calls
General Format of Method Definition General Format of Method Calls
classname. methodName (arguments);
public static / static returnType methodName OR
(parameters) { methodName (arguments);
// method body
}
Type 3 : A method definition which return int Type 3 : receive value, pass no
value, receive no parameters arguments
public static int calcProduct( ) {
return 10*10; calcProduct ( ) ;
}
Type 4 : A method definition which return Type 4 : receive value, pass
int value, receive parameters arguments
static int calcAdd(int x, int y) {
return x+y; calcAdd (x, y) ;
}
, Example Coding Using Type 3
class Calculation { Output :
The answer for k is: 17.0
public static void main (String [ ] args){
double result;
result = Calculation. getcalc ( );
System.out.println("The answer for k is:" +result); }
public static double getcalc ( ) {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; } }
Return value type double
Method name getcalc
Formal parameter(passed arguments) ()
Actual parameter(arguments) ()
Parameter list ()
Return value k
Method header public static double getcalc ( ) { }
Method call Calculation .getcalc ( ) ;
Method body {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; }
className Calculation
Method definition public static double getcalc ( ) {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; }
3.5 METHOD
(METHOD DEFINITION and METHOD CALL for type 3 and Type 4)
Method Definition Method Calls
General Format of Method Definition General Format of Method Calls
classname. methodName (arguments);
public static / static returnType methodName OR
(parameters) { methodName (arguments);
// method body
}
Type 3 : A method definition which return int Type 3 : receive value, pass no
value, receive no parameters arguments
public static int calcProduct( ) {
return 10*10; calcProduct ( ) ;
}
Type 4 : A method definition which return Type 4 : receive value, pass
int value, receive parameters arguments
static int calcAdd(int x, int y) {
return x+y; calcAdd (x, y) ;
}
, Example Coding Using Type 3
class Calculation { Output :
The answer for k is: 17.0
public static void main (String [ ] args){
double result;
result = Calculation. getcalc ( );
System.out.println("The answer for k is:" +result); }
public static double getcalc ( ) {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; } }
Return value type double
Method name getcalc
Formal parameter(passed arguments) ()
Actual parameter(arguments) ()
Parameter list ()
Return value k
Method header public static double getcalc ( ) { }
Method call Calculation .getcalc ( ) ;
Method body {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; }
className Calculation
Method definition public static double getcalc ( ) {
int x = 5;
double y = 3.0, z = 2.0, k ;
k = (x*y)+z;
return k; }