Monday, June 1, 2015

Methods


// When to make the methods?
// Is that the single operation you want to do
// Does it needs several steps to do that exact operations

public class MethodDemo1 {

public static void main(String[] args) {

add();
add(20, 30);

int res = add(30, 40, 50);
System.out.println("SmartMethod: "+res);

}

// DUMB METHOD: When calling this method it always add 10 and 20 and
// print the result in console and do not return anything

public static  void add() {
int a=10,b=20;
int result = a + b;
System.out.println("DumMethod: "+result);
}

// CLEVER METHOD: When calling this method it ask user to what numbers
// are to add and print the  result in console and do not return anything

public static void add(int a, int b){
int result = a + b;
System.out.println("CleverMethod: "+result);
}

// SMART METHOD: When calling this method it ask user to what numbers are
// to add and  it return the int value which is first not shown in the console and
// it can later on put in certain int variable and display as an output using syso

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


No comments:

Post a Comment