40+ examples of user defined methods in java with explanation

download 40+ examples of user defined methods in java with explanation

of 100

  • date post

    07-Dec-2014
  • Category

    Education

  • view

    909
  • download

    3

Embed Size (px)

description

40+ examples of user defined methods in java with explanation, advantages, terms, exercises, pass arrays to methods, return array from methods, for each loop examples, build complex programs using your defined methods animation included INDEX Simple Programs using Methods Add two numbers Average of three numbers Circle area Fahrenheit to Celsius Check number is even or not Check number is prime or not Reverse of a number Check number is palindrome or not Count number of digits in a number Sum of digits Pass Array to Methods Print all elements of an array Sum of an array Average of an array Maximum element of an array Minimum element of an array Returning Array From Methods Reverse array Reverse every element of an array Decimal to binary, octal, hex Binary to decimal, octal, hex Octal to binary,decimal, hex Hexadecimal to binary, octal, decimal

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

  • 1. User Defined Methods in Java With Animation

2. Index 1. 2.Advantages Simple Programs using Methods a) b) c) d) e) f) g) h) i) j) k) l) m) n) o) p) q) r)Add two numbers Average of three numbers Circle area Fahrenheit to Celsius Check number is even or not Check number is prime or not Reverse of a number Check number is palindrome or not Count number of digits in a number Sum of digits LCM of 2, 3 and 4 numbers HFC of 2 numbers 1 to 100 prime numbers 200 to 500 prime and palindrome Decimal to binary, octal, hex Binary to decimal, octal, hex Octal to binary,decimal, hex Hexadecimal to binary, octal, decimal3.Pass Array to Methods a) b) c) d) e)4.Print all elements of an array Sum of an array Average of an array Maximum element of an array Minimum element of an arrayReturning Array From Methods a) b)Reverse array Reverse every element of an array 3. Function v/s Method 4. Function v/s Method Q.)What is the difference between a function and a method.? 5. 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. 6. 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 class only. 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. 7. Function with no arguments and no return(no input no output) void display() { System.out.println("this is inside method body");} 8. 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 9. Function with no arguments and no return(no input no output) Name of the function is displayvoid display() { System.out.println("this is inside method body");} When you want to return no value, then set return type to void 10. Function with no arguments and no return(no input no output) Name of the function is displayNo arguments herevoid display() { System.out.println("this is inside method body");} When you want to return no value, then set return type to void 11. Complete Program public 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"); } } 12. Method DefinitionComplete 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"); } } 13. Method DefinitionComplete 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 Calling 14. Add two integer numbers int add(int a, int b) { int c=a+b; return c; } 15. Add two integer numbers int add(int a, int b) { int c=a+b; return c; }Two Integer Arguments 16. Add two integer numbers int add(int a, int b) { int c=a+b; return c; } Returning integer value to calling methodTwo Integer Arguments 17. 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)); } } 18. Display method is called method, because it is called by main methodComplete Programpublic 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)); } } 19. Display method is called method, because it is called by main methodComplete Programpublic 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)); } }main method is calling method, because it is calling display method. 20. Average of three integer numbers double { double d; d=((double)a+b+c)/3; return d; }) 21. Average of three integer numbers double { double d; d=((double)a+b+c)/3; return d; })This method takes multiple arguments and returns single value 22. Average of three integer numbers double { double d; d=((double)a+b+c)/3; return d; })This method takes multiple arguments and returns single value 23. Calculate circle Area double { double area=Math.PI*radius*radius; return area; } 24. Calculate circle Area double { double area=Math.PI*radius*radius; return area; } 25. Convert Fahrenheit to Celsius double convertFahToCel(double fah) { double cel=(fah-32)*5/9; return cel;} 26. 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 27. Check number is even or not boolean checkEven(int n) { if(n%2==0) { return true; } else { return false; } } 28. Check number is even or not boolean checkEven(int n) { if(n%2==0) { return true; } else { return false; } } 29. Check number is prime or not boolean isPrime(int n) { int i; for( i=2;i=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()); } 57. Convert Decimal to Binary String convertDecimalToBinary(long n) { int base=2; String ans=convertDecimalToXXX(n,base); return ans; } 58. Convert Decimal to Octal String convertDecimalToOctal(long n) { int octal_base=8; String ans=convertDecimalToXXX(n,octal_base); return ans; } 59. Convert Decimal to Hexadecimal String convertDecimalToHexadecimal(long n) { int hex_base=16; String ans=convertDecimalToXXX(n,hex_base); return ans; } 60. Convert Binary to XXX long 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; } 61. Convert Binary to Decimal long convertBinaryToDecimal(String binary_number) { int binary_base=2; long ans=convertXXXToDecimal(binary_base,binary_number); return ans; } 62. Convert Octal to Decimal long convertOctalToDecimal(String binary_number) { int binary_base=8; long ans=convertXXXToDecimal(binary_base,binary_number); return ans; } 63. Convert Hexadecimal to Decimal long convertHexToDecimal(String binary_number) { int binary_base=2; long ans=convertXXXToDecimal(binary_base,binary_number); return ans; } 64. Convert Hexadecimal to Octal String convertHexToOctal(String hexNumber) { long decimal=convertHexToDecimal(hexNumber); String ans=convertDecimalToOctal(decimal); return ans; } 65. Convert Hexadecimal to Binary String convertHexToBinary(String hexNumber) { long decimal=convertHexToDecimal(hexNumber); String ans=convertDecimalToBinary(decimal); return ans; } 66. Convert Octal to Hexadecimal String convertOctalToHex(String octalNumber) { long decimal=convertOctalToDecimal(octalNumber); String ans=convertDecimalToHex(decimal); return ans; } 67. Convert Octal to Binary String convertOctalToBinary(String octalNumber) { long decimal=convertOctalToDecimal(octalNumber); String ans=convertDecimalToBinary(decimal); return ans; } 68. Print All elements of an Array void arrayTraversing(int arr[]) { for(int x:arr) { System.out.println(x); } } 69. Print All elements of an Array void arrayTraversing(int arr[]) { for(int x:arr) { System.out.println(x); } } 70. Print All elements of an Array Method Header / Method Declarationvoid arrayTraversing(int arr[]) { for(int x:arr) { System.out.println(x); } } 71. Sum of an Array int sumOfArray(int arr[]) { int sum=0; for(int x:arr) { sum=sum+x; } return sum; } 72. Sum of an Array int sumOfArray(int arr[]) { int sum=0; for(int x:arr) { sum=sum+x; Taking an integer array and returning } single value of int data type return sum; } 73. 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; } 74. Maximum of an Array byte maximumOfArray(byte arr[]) { byte max=Byte.MIN_VALUE; for(byte x:arr) { if(x>max) { max=x; } } return max; } 75. Maximum of an Array byte maximumOfArray(byte arr[]) { byte max=Byte.MIN_VALUE; for(byte x:arr) { if(x>max) { max=x; } } return max; } 76. Minimum of an Array byte minimumOfArray(byte arr[]) { byte min=Byte.MAX_VALUE; for(byte x:arr) { if(x=0; i-- , j++) { This method takes an xyz[i]=arr[j]; array(multiple byte } type values) and returns an return xyz; array(multiple byte } type values). 82. Original Array 5 78Reverse array 891655byte[] reverseArray(byte arr[]) { Returning byte array to calling method byte xyz[]=new byte[100]; int i=arr.length-1; for(j=0; i>=0; i-- , j++) { This method takes an xyz[i]=arr[j]; array(multiple byte } type values) and returns an return xyz; array(multiple byte } type values). 83. Original Array 5 78Reverse array 8916551655Converted Array 89785byte[] reverseArray(byte arr[]) { Returning byte array to calling method byte xyz[]=new byte[100]; int i=arr.length-1; for(j=0; i>=0; i-- , j++) { This method takes an xyz[i]=arr[j]; array(multiple byte } type values) and returns an return xyz; array(multiple byte } type values). 84. Original Array 5 78Reverse array 8916551655Converted Array 89785byte[] reverseArray(byte arr[]) { Returning byte array to calling method byte xyz[]=new byte[100]; int i=arr.length-1; for(j=0; i>=0; i-- , j++) { This method takes an xyz[i]=arr[j]; array(mul