Download - 40+ examples of user defined methods in java with explanation

Transcript
Page 1: 40+ examples of user defined methods in java with explanation

User Defined Methods in Java

With Animation

Page 2: 40+ examples of user defined methods in java with explanation

Index1. Advantages2. Simple Programs using Methods

a) Add two numbersb) Average of three numbersc) Circle aread) Fahrenheit to Celsiuse) Check number is even or notf) Check number is prime or notg) Reverse of a numberh) Check number is palindrome or noti) Count number of digits in a numberj) Sum of digitsk) LCM of 2, 3 and 4 numbersl) HFC of 2 numbersm) 1 to 100 prime numbersn) 200 to 500 prime and palindromeo) Decimal to binary, octal, hexp) Binary to decimal, octal, hexq) Octal to binary,decimal, hexr) Hexadecimal to binary, octal, decimal

3. Pass Array to Methodsa) Print all elements of an arrayb) Sum of an arrayc) Average of an arrayd) Maximum element of an arraye) Minimum element of an array

4. Returning Array From Methodsa) Reverse arrayb) Reverse every element of an array

Page 3: 40+ examples of user defined methods in java with explanation

Function v/s Method

 

 

 

 

Page 4: 40+ examples of user defined methods in java with explanation

Function v/s Method

Q.)What is the difference between a function and a method.? 

 

 

Page 5: 40+ examples of user defined methods in java with explanation

Function v/s Method

Q.)What is the difference between a function and a method.?Ans.) A method is a function that is written in a class.

 

 

Page 6: 40+ examples of user defined methods in java with explanation

Function v/s Method

Q.)What is the difference between a function and a method.?Ans.) A method is a function that is written in a class.

We do not have functions in java; instead we have methods. This means whenever a function is written in java. It should be written inside the classonly. But if we take C++, we can write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

Page 7: 40+ examples of user defined methods in java with explanation

Function with no arguments and no return(no input no output)

void display() { System.out.println("this is inside method body");

}

Page 8: 40+ examples of user defined methods in java with explanation

Function with no arguments and no return(no input no output)

void display() { System.out.println("this is inside method body");

}

When you want to return no value, then set return type to void

Page 9: 40+ examples of user defined methods in java with explanation

Function with no arguments and no return(no input no output)

void display() { System.out.println("this is inside method body");

}

Name of the function is “display”

When you want to return no value, then set return type to void

Page 10: 40+ examples of user defined methods in java with explanation

No arguments here

Function with no arguments and no return(no input no output)

void display() { System.out.println("this is inside method body");

}

Name of the function is “display”

When you want to return no value, then set return type to void

Page 11: 40+ examples of user defined methods in java with explanation

Complete Programpublic class function1 { static void display() { System.out.println("this is inside method body"); } public static void main(String args[]) { System.out.println("before function call");

display();

System.out.println("after function call"); }}

Page 12: 40+ examples of user defined methods in java with explanation

Complete Programpublic class function1 { static void display() { System.out.println("this is inside method body"); } public static void main(String args[]) { System.out.println("before function call");

display();

System.out.println("after function call"); }}

Method Definition

Page 13: 40+ examples of user defined methods in java with explanation

Complete Programpublic class function1 { static void display() { System.out.println("this is inside method body"); } public static void main(String args[]) { System.out.println("before function call");

display();

System.out.println("after function call"); }}

Method Definition

Method Calling

Page 14: 40+ examples of user defined methods in java with explanation

Add two integer numbers

int add(int a, int b){

int c=a+b;return c;

}

Page 15: 40+ examples of user defined methods in java with explanation

Add two integer numbers

int add(int a, int b){

int c=a+b;return c;

}

Two Integer Arguments

Page 16: 40+ examples of user defined methods in java with explanation

Add two integer numbers

int add(int a, int b){

int c=a+b;return c;

}

Two Integer Arguments

Returning integer value to calling method

Page 17: 40+ examples of user defined methods in java with explanation

Complete Program

public class function1 { static int add(int a, int b) {

int c=a+b;return c;

} public static void main(String args[]) {

System.out.println(add(45,67)); }}

Page 18: 40+ examples of user defined methods in java with explanation

Complete Program

public class function1 { static int add(int a, int b) {

int c=a+b;return c;

} public static void main(String args[]) {

System.out.println(add(45,67)); }}

Display method is called method,

because it is called by main

method

Page 19: 40+ examples of user defined methods in java with explanation

Complete Program

public class function1 { static int add(int a, int b) {

int c=a+b;return c;

} public static void main(String args[]) {

System.out.println(add(45,67)); }}

Display method is called method,

because it is called by main

method

main method is calling method,

because it is calling display

method.

Page 20: 40+ examples of user defined methods in java with explanation

Average of three integer numbers

double avg(int a, int b, int c){

double d;d=((double)a+b+c)/3;

return d;}

Page 21: 40+ examples of user defined methods in java with explanation

Average of three integer numbers

double avg(int a, int b, int c){

double d;d=((double)a+b+c)/3;

return d;}

This method takes multiple arguments and

returns single value

Page 22: 40+ examples of user defined methods in java with explanation

Method Signature—the method's name and the parameter types.

Average of three integer numbers

double avg(int a, int b, int c){

double d;d=((double)a+b+c)/3;

return d;}

This method takes multiple arguments and

returns single value

Page 23: 40+ examples of user defined methods in java with explanation

Calculate circle Area

double circleArea(float radius){ double area=Math.PI*radius*radius; return area;}

Page 24: 40+ examples of user defined methods in java with explanation

Calculate circle Area

double circleArea(float radius){ double area=Math.PI*radius*radius; return area;}

Method Signature—the method's name and the parameter types.

Page 25: 40+ examples of user defined methods in java with explanation

Convert Fahrenheit to Celsius

double convertFahToCel(double fah){double cel=(fah-32)*5/9;return cel;

}

Page 26: 40+ examples of user defined methods in java with explanation

Convert Fahrenheit to Celsius

double convertFahToCel(double fah){double cel=(fah-32)*5/9;return cel;

}

This method takes one argument and returns

single value

Page 27: 40+ examples of user defined methods in java with explanation

Check number is even or not

boolean checkEven(int n){

if(n%2==0){

return true;}else{

return false;}

}

Page 28: 40+ examples of user defined methods in java with explanation

Method Signature—the method's name and the parameter types.

Check number is even or not

boolean checkEven(int n){

if(n%2==0){

return true;}else{

return false;}

}

Page 29: 40+ examples of user defined methods in java with explanation

Check number is prime or notboolean isPrime(int n){ int i; for( i=2;i<n;i++) { if(n%i==0) { break; } }d if(n==i) { return true; } else { return false; }}

Page 30: 40+ examples of user defined methods in java with explanation

Reverse number

int reverseNumber(int n){

int x=0;for( ; n!=0 ; ){

int r=n%10;x=x*10+r;n=n/10;

} return x;}

Page 31: 40+ examples of user defined methods in java with explanation

Check number is palindrome or notboolean isPalindrome(int n){ int rev = reverseNumber(n) ; if(n==rev) { return true; } else { return false; }}

FOCUS ON ONE WORKWe need to focus on palindrome

not on reverse number code, this is a advantage of method.

Page 32: 40+ examples of user defined methods in java with explanation

Check number is palindrome or notboolean isPalindrome(int n){ int rev = reverseNumber(n) ; if(n==rev) { return true; } else { return false; }}

Using previous slide’s reverse number method

FOCUS ON ONE WORKWe need to focus on palindrome

not on reverse number code, this is a advantage of method.

Page 33: 40+ examples of user defined methods in java with explanation

Count number of digits in a number

This is Exercise

Page 34: 40+ examples of user defined methods in java with explanation

Sum of digits

This is Exercise

Page 35: 40+ examples of user defined methods in java with explanation

LCM(Lowest Common Multiple) of 2 numbers

long getLCM(int n1, int n2){ long answer=1; for(int i=2;n1!=1 && n2!=1;) { if(n1%i==0 && n2%i==0) { n1=n1/i; n2=n2/i; answer=answer*i; } else if(n1%i==0) { n1=n1/i; answer=answer*i; }

else if(n2%i==0) { n2=n2/i; answer=answer*i; } else { i++; }

}//end of for loop

answer=n1*n2*answer; return answer;

}//end of this method

Page 36: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Page 37: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Method overloading on getLCM() method differ by number of arguments

Page 38: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Page 39: 40+ examples of user defined methods in java with explanation

User Defined getLCM() 2 argument

method

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Page 40: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Page 41: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;} Narrowing

type conversion

Page 42: 40+ examples of user defined methods in java with explanation

LCM of 3 Numbers

long getLCM(int n1,int n2,int n3){ long result1 = getLCM(n1,n2);

long finalResult=getLCM((int) result1,n3);

return finalResult;}

Page 43: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Page 44: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Method overloading on getLCM() method differ by number of arguments

Page 45: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Page 46: 40+ examples of user defined methods in java with explanation

User Defined getLCM() 3

arguments method

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Page 47: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Page 48: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;} Narrowing

type conversion

Page 49: 40+ examples of user defined methods in java with explanation

LCM of 4 Numbers

long getLCM(int n1,int n2,int n3, int n4){ long result1 = getLCM(n1,n2,n3);

long finalResult=getLCM((int) result1,n4);

return finalResult;}

Page 50: 40+ examples of user defined methods in java with explanation

HCF(Highest Common Factor) of 2 numbers

int getHCF(int n1,int n2){

int lcmOfThese=(int) getLCM(n1,n2);long product=n1*n2;int hcfOfThese=(int)(product/lcmOfThese);return hcfOfThese;

}

Page 51: 40+ examples of user defined methods in java with explanation

HCF(Highest Common Factor) of 2 numbers

int getHCF(int n1,int n2){

int lcmOfThese=(int) getLCM(n1,n2);long product=n1*n2;int hcfOfThese=(int)(product/lcmOfThese);return hcfOfThese;

}

Narrowing type conversion /manual type

casting/ down casting

Page 52: 40+ examples of user defined methods in java with explanation

HCF(Highest Common Factor) of 2 numbers

int getHCF(int n1,int n2){

int lcmOfThese=(int) getLCM(n1,n2);long product=n1*n2;int hcfOfThese=(int)(product/lcmOfThese);return hcfOfThese;

}

Page 53: 40+ examples of user defined methods in java with explanation

HCF(Highest Common Factor) of 2 numbers

int getHCF(int n1,int n2){

int lcmOfThese=(int) getLCM(n1,n2);long product=n1*n2;int hcfOfThese=(int)(product/lcmOfThese);return hcfOfThese;

}

User Defined getLCM() method

Page 54: 40+ examples of user defined methods in java with explanation

Print prime numbers between 1 and 100

void printPrime1To100(){

for(int i=1;i<=100;i++){

if(isPrime(i)==true) { System.out.println(i); }

}}

User defined method

Page 55: 40+ examples of user defined methods in java with explanation

Print prime numbers between 200 and 500 which are Palindrome numbers too

This is exercise

Page 56: 40+ examples of user defined methods in java with explanation

Convert Decimal to XXXString convertDecimalToXXX(long n,int base){ StringBuilder sb = new StringBuilder(); for(;n!=0;n=n/base) { byte x= (byte)(n%base); if(x>=10) { char ch=' '; switch(x) { case 10:ch='A';break; case 11:ch='B';break; case 12:ch='C';break; case 13:ch='D';break; case 14:ch='E';break; case 15:ch='F';break; }

sb.append(ch); } else { sb.append(x); }}return String.valueOf(sb.reverse());}

Page 57: 40+ examples of user defined methods in java with explanation

Convert Decimal to Binary

String convertDecimalToBinary(long n){

int base=2;String

ans=convertDecimalToXXX(n,base);return ans;

}

Page 58: 40+ examples of user defined methods in java with explanation

Convert Decimal to OctalString convertDecimalToOctal(long n){ int octal_base=8; String ans=convertDecimalToXXX(n,octal_base); return ans;}

Page 59: 40+ examples of user defined methods in java with explanation

Convert Decimal to HexadecimalString convertDecimalToHexadecimal(long n){ int hex_base=16; String ans=convertDecimalToXXX(n,hex_base); return ans;}

Page 60: 40+ examples of user defined methods in java with explanation

Convert Binary to XXXlong convertXXXToDecimal(int base,String num){ StringBuilder sb = new StringBuilder(num); long sum=0,t=0; for(int i=sb.length()-1;sb.length()!=0;i--,t++) { Character x=sb.charAt(i); int y=x-48; sb=sb.deleteCharAt(i); double z=y*(Math.pow(base, t)); sum=sum+(long)z; } return sum;}

Page 61: 40+ examples of user defined methods in java with explanation

Convert Binary to Decimallong convertBinaryToDecimal(String binary_number){ int binary_base=2; long ans=convertXXXToDecimal(binary_base,binary_number); return ans;}

Page 62: 40+ examples of user defined methods in java with explanation

Convert Octal to Decimallong convertOctalToDecimal(String binary_number){ int binary_base=8; long ans=convertXXXToDecimal(binary_base,binary_number); return ans;}

Page 63: 40+ examples of user defined methods in java with explanation

Convert Hexadecimal to Decimallong convertHexToDecimal(String binary_number){

int binary_base=2;long ans=convertXXXToDecimal(binary_base,binary_number);return ans;

}

Page 64: 40+ examples of user defined methods in java with explanation

Convert Hexadecimal to Octal String convertHexToOctal(String hexNumber){

long decimal=convertHexToDecimal(hexNumber);String ans=convertDecimalToOctal(decimal);return ans;

}

Page 65: 40+ examples of user defined methods in java with explanation

Convert Hexadecimal to BinaryString convertHexToBinary(String hexNumber){

long decimal=convertHexToDecimal(hexNumber);String ans=convertDecimalToBinary(decimal);return ans;

}

Page 66: 40+ examples of user defined methods in java with explanation

Convert Octal to HexadecimalString convertOctalToHex(String octalNumber){ long decimal=convertOctalToDecimal(octalNumber); String ans=convertDecimalToHex(decimal); return ans;}

Page 67: 40+ examples of user defined methods in java with explanation

Convert Octal to BinaryString convertOctalToBinary(String octalNumber){ long decimal=convertOctalToDecimal(octalNumber); String ans=convertDecimalToBinary(decimal); return ans;}

Page 68: 40+ examples of user defined methods in java with explanation

Pass Array to Called Method

Page 69: 40+ examples of user defined methods in java with explanation

Print All elements of an Arrayvoid arrayTraversing(int arr[]){

for(int x:arr) {

System.out.println(x);}

}

Page 70: 40+ examples of user defined methods in java with explanation

Print All elements of an Arrayvoid arrayTraversing(int arr[]){

for(int x:arr) {

System.out.println(x);}

}

Page 71: 40+ examples of user defined methods in java with explanation

Print All elements of an Arrayvoid arrayTraversing(int arr[]){

for(int x:arr) {

System.out.println(x);}

}

Method Header /Method

Declaration

Page 72: 40+ examples of user defined methods in java with explanation

Sum of an Array

int sumOfArray(int arr[]){

int sum=0; for(int x:arr) { sum=sum+x; } return sum;}

Page 73: 40+ examples of user defined methods in java with explanation

Sum of an Array

int sumOfArray(int arr[]){

int sum=0; for(int x:arr) { sum=sum+x; } return sum;}

Taking an integer array and returning single value of int

data type

Page 74: 40+ examples of user defined methods in java with explanation

Average of an Array

float averageOfArray(int arr[]){

int sum=0;float avg;

for(int x:arr) { sum=sum+x; }

avg=(float)sum/arr.length; return avg;}

Page 75: 40+ examples of user defined methods in java with explanation

Maximum of an Arraybyte maximumOfArray(byte arr[]){

byte max=Byte.MIN_VALUE;for(byte x:arr)

{ if(x>max) { max=x; } } return max;}

Page 76: 40+ examples of user defined methods in java with explanation

Maximum of an Arraybyte maximumOfArray(byte arr[]){

byte max=Byte.MIN_VALUE;for(byte x:arr)

{ if(x>max) { max=x; } } return max;}

Minimum value of

byte is -128.

Page 77: 40+ examples of user defined methods in java with explanation

Minimum of an Arraybyte minimumOfArray(byte arr[]){

byte min=Byte.MAX_VALUE;for(byte x:arr)

{ if(x<min) { min=x; } } return min;}

Page 78: 40+ examples of user defined methods in java with explanation

Minimum of an Arraybyte minimumOfArray(byte arr[]){

byte min=Byte.MAX_VALUE;for(byte x:arr)

{ if(x<min) { min=x; } } return min;}

Wrapper class for byte data type

Page 79: 40+ examples of user defined methods in java with explanation

Minimum of an Arraybyte minimumOfArray(byte arr[]){

byte min=Byte.MAX_VALUE;for(byte x:arr)

{ if(x<min) { min=x; } } return min;}

Maximum value of

byte is 127.

Wrapper class for byte data type

Page 80: 40+ examples of user defined methods in java with explanation

Return Array from Called Method

Page 81: 40+ examples of user defined methods in java with explanation

Return prime numbers between 1 and 100

int[] getPrime1To100(){

int arr[] = new int[100];for(int i=1,j=1;i<=100;i++){

if(isPrime(i)==true) { arr[j]=i;

j++; }

}return arr;

}

Taking no parameters but returning an array.

A method cannot return multiple

values but it can return an array.

Page 82: 40+ examples of user defined methods in java with explanation

Complete Programpublic class function1 {

static int[] getPrime1To100(){

int arr[] = new int[100];for(int i=1,j=1;

i<=100;i++){

if(isPrime(i)==true) { arr[j]=i;

j++; }

} return arr;}

public static void main(String args[]) { int art[]=getPrime1To100(); for(int x:art) { if(x!=0) { System.out.println(x); } } }//end of main method}//end of public class

Array contains 100 values,

some are prime numbers other

are default values (0).

Page 83: 40+ examples of user defined methods in java with explanation

Reverse array

byte[] reverseArray(byte arr[]){

byte xyz[]=new byte[100];int i=arr.length-1;for(j=0; i>=0; i-- , j++){

xyz[i]=arr[j];}return xyz;

}

Returning byte array to calling method

This method takes an array(multiple byte

type values) and returns an

array(multiple byte type values).

Page 84: 40+ examples of user defined methods in java with explanation

Reverse array

byte[] reverseArray(byte arr[]){

byte xyz[]=new byte[100];int i=arr.length-1;for(j=0; i>=0; i-- , j++){

xyz[i]=arr[j];}return xyz;

}

Returning byte array to calling method

5 78 89 1655Original Array

This method takes an array(multiple byte

type values) and returns an

array(multiple byte type values).

Page 85: 40+ examples of user defined methods in java with explanation

Reverse array

byte[] reverseArray(byte arr[]){

byte xyz[]=new byte[100];int i=arr.length-1;for(j=0; i>=0; i-- , j++){

xyz[i]=arr[j];}return xyz;

}

Returning byte array to calling method

5 78 89 1655Original Array

1655 89 78 5Converted Array

This method takes an array(multiple byte

type values) and returns an

array(multiple byte type values).

Page 86: 40+ examples of user defined methods in java with explanation

Reverse array

byte[] reverseArray(byte arr[]){

byte xyz[]=new byte[100];int i=arr.length-1;for(j=0; i>=0; i-- , j++){

xyz[i]=arr[j];}return xyz;

}

Returning byte array to calling method

5 78 89 1655Original Array

1655 89 78 5Converted Array

This method takes an array(multiple byte

type values) and returns an

array(multiple byte type values).

Page 87: 40+ examples of user defined methods in java with explanation

Reverse digits of all array elements 1

ExampleOriginal Array

Converted Array

5 78 89 1655 464 782 346 75623

5 87 98 5561 464 287 643 32657

Page 88: 40+ examples of user defined methods in java with explanation

Reverse digits of all array elements 2

void reverseEveryArrayElement(int a[]){ for(int i=0;i<a.length;i++) { a[i]=reverseNumber(a[i]); }}

Example

Page 89: 40+ examples of user defined methods in java with explanation

Reverse digits of all array elements 2

void reverseEveryArrayElement(int a[]){ for(int i=0;i<a.length;i++) { a[i]=reverseNumber(a[i]); }}

Replacing every element of array with

its reverse number Example

Page 90: 40+ examples of user defined methods in java with explanation

Reverse digits of all array elements 2

void reverseEveryArrayElement(int a[]){ for(int i=0;i<a.length;i++) { a[i]=reverseNumber(a[i]); }}

This method is modifying

original array elements.

Replacing every element of array with

its reverse number Example

Page 91: 40+ examples of user defined methods in java with explanation

Complete Programpublic static void main(String args[]){

int a[]={5,78,89,1655,464,782,346,75623}; reverseEveryArrayElement(a);

arrayTraversing(a);}static void reverseEveryArrayElement(int a[]){

for(int i=0;i<a.length;i++) { a[i]=reverseNumber(a[i]); }}

Page 92: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

 

 

48

Page 93: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

1. To help make the program more understandable

 

 

48

Page 94: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

1. To help make the program more understandable

2. To modularize the tasks of the program– building blocks of the program

 

48

Page 95: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

1. To help make the program more understandable

2. To modularize the tasks of the program– building blocks of the program

3. Write a module once– those lines of source code are called multiple times

in the program

48

Page 96: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

     

49

Page 97: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

4. While working on one function, you can focus on just that part of the program – construct it, – debug it, – perfect it.

49

Page 98: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

4. While working on one function, you can focus on just that part of the program – construct it, – debug it, – perfect it.

5. Different people can work on different functions simultaneously.

49

Page 99: 40+ examples of user defined methods in java with explanation

Advantages of Using Methods

4. While working on one function, you can focus on just that part of the program – construct it, – debug it, – perfect it.

5. Different people can work on different functions simultaneously.

6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times

49

Page 100: 40+ examples of user defined methods in java with explanation

Thank You