Tuesday, 31 January 2012

Searching using method

  1. Write starting curly braces of main method.
  2. declare an array of size 5 lets say int[] a; a=new int[5];
  3. declare an integer variable b.
  4. Write a caller statement which pass the array a to the user defined method. first choose an  appropriate name of method like searcharray.
  5. b=searcharray(a); when the value will be return by method then b will recieve it.
  6. write conditional funtions like if  and else.  if(b==-1) then print "number not found" in body of if and in body of  else print "position of number is b".
  7. write closing curly braces of method main .
  8. write method havind name searcharray and return type int and should recieve array public static int searcharray(int[] x). x array will recieve the contents of array a.
  9. write starting curly braces of method searcharray.
  10. Take input in array x using loop having variable name lets say q which will start from 0 to x.length and increment by 1.
  11. inside the body of loop write x[q]=EasyIn.getInt();
  12. Now take a number from user and then we will have to give its position in the array
  13. declare variable of int type lets say e and w=0;
  14. start a loop having variable name i start from 0 to x.length and increment by 1.
  15. Write starting curly braces of this loop
  16. Write if(x[i]==e){
  17. within the body of if write w=i+1; and then break;
  18. write closing curly braces of if
  19. write closing curly braces of loop
  20. write return statement which return w . It will return the value to b written before caller.
  21. write closing curly braces of method searcharray.
Note: main method and user defined method should be inside the body of class.

    No comments:

    Post a Comment