Tuesday, 31 January 2012

Scary trick ( swaping without declaring third variable )

  1. Initialize two integers a and b with the values of your desire
  2. a=a+b;
  3. b=a-b;
  4. a=a-b;
  5. 2nd, 3rd and 4th steps will swap the values of a and b. value of a will be shifted to b and value of b will be shifted to a. Enjoy it.

Sorting using method

  1. declare an array of size 5. having name a.
  2. take input by writing a[b]=EasyIn........ in it using loop having variable name b starting from 0 to a.length and increment by 1
  3. pass this array to the the method having name methsort like methsort(a);
  4. write closing curly braces of main method.
  5. write method like public static void methsort(int[] x)
  6. write starting curly braces of method methsort.
  7. initialize a variable e=0;
  8. start a loop having variable c from 0 to less then 4 and then write starting curly braces of it
  9. write an other loop having variable name d from 0 to less then 4 and then write starting curly braces of it
  10. write if(x[d]>x[d+1]) and write starting curly braces of if 
  11. with in the body of if write e=x[d];x[d]=x[d+1];x[d+1]=e; write closing curly braces of if, second loop and first loop.
  12. For the output write an other loop having variable name f from 0 to 4 and increment by 1. and with in the body of this loop  write output statement ..........print("  "+x[f]);
  13. write closing curly brace of this loop.
 Note: This code will produce the numbers in ascending order and for descending order replace greater then sign with less then sign in point 10 if condition.

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.

    Qudratic equation using method

    1. At the top of code, write " import java.lang.Math.*;" this will import a math class and will be helpful to find mathematical problems.
    2.  And then class and main as we already know and now write starting curly braces of main method.
    3. Declare three variables a,b and c of int type.
    4. Take three values of a,b and c by using easyin method as these values are required for quadratic equation.
    5. Write a caller which pass three values a,b and c and choose an appropriate name of if like quadii(a,b,c);
    6. Now, write the closing curly braces of main method.
    7. Write method which take three values of a,b and c like public static void quadii(int x, int y, int z). Here x y and z are different from a b and c these can be same also but here values of a,b and c have been transferred into x y and z.
    8.  Write starting curly braces of method quadii.
    9. declare some variables lets say d e f g
    10. And four double type o u v
    11. d=y*y;
    12. e=4*x*z;
    13. f=d-e;
    14. g=2*x;
    15. o=(double)Math.sqrt(f); ( this will find the square root of f and double will convert the value of f into double )
    16. u=(-y+o)/g;
    17. v=(-y-o)/g;
    18. Now u and v are the two value of quadratic equation and now write output statement.
    19. System.out.println("Solution set = { "+u+" , "+v+"}");
    20. Now write the closing curly braces of method quadii.
    Note: main method and user defined method should be inside the body of class at the top.

    Monday, 30 January 2012

    Computing factorial using method

    1. Start main body with starting curly braces and declare two variables lets say a and b.
    2. Take input inside a using easyin method.
    3. choose a proper name of method lets say factii and call the method and assign value of it to b. like b=factii(a);
    4. When compiler will read the caller it will find the method factii from the code and will reach there.
    5. write output statement that prints the final value. like ..............println(" "+b);
    6. Write ending curly braces.
    7. Make a method have name factii and one parametre of int type and should have int return type like public static int factii(int x). This x will take the value of a from the caller in the 3rd point.
    8. Initialize a variable lets say c =1
    9.  Write starting curly braces and then Start a loop from 1 to less then x and increment it by 1.
    10. Write starting curly braces of this loop.
    11. Inside loop write a statement like c=c*x;
    12. Write ending curly braces of method factii.
    13. Write return statement like return c; so it will return  the final value of c to the b from the caller and then on the fourth line value of b will be showed.
    Note: Method main and user defined method should be inside the body of class.

    Making triangle of stars using loops

    *
    **
    ***
    ****
    *****
    1. Start first loop with a variable name lets say "a" from 1 to5  and increment it by 1 and write starting  curly braces of it.
    2. Start second loop with a variable name lets say "b" from 1 to less then a and increment it by 1 and this loop should be the nested of first loop.
    3. Write starting curly braces of second loop  and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of second loop.
    4. Write statement for line break outside the body of second loop and inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
    *****
    ****
    ***
    **
    *

    1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting  curly braces of it.
    2. Start second loop with a variable name lets say "b" from 5 to greater then a and decrement it by 1 and this loop should be the nested of first loop.
    3. Write starting curly braces of second loop  and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of second loop.
    4. Write statement for line break outside the body of second loop and inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
            *
          **
        ***
      ****
    *****

    1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting curly braces of it.
    2. Start second loop with a variable name lets say "b" from 5 to greater then a and decrement it by 1 and this loop should be the nested of first loop.
    3. Write starting curly braces of second loop and then write output statement which prints one space like ....................print(" "); ( note: don't write println here) and then write ending curly braces of second loop.
    4. Start third loop with a variable name lets say "c" from 1 to less then a and increment it by 1 and this loop should also be the nested of first loop but not of second loop.
    5. Write starting curly braces of third loop and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of third loop.
    6. Write statement for line break outside the body of second loop and third loop but inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.
    *****
      ****
        ***
          **
            *

    1. Start first loop with a variable name lets say "a" from 1 to 5 and increment it by 1 and write starting curly braces of it.
    2. Start second loop with a variable name lets say "b" from 1 to less then a and increment it by 1 and this loop should be the nested of first loop.
    3. Write starting curly braces of second loop and then write output statement which prints one space like ....................print(" "); ( note: don't write println here) and then write ending curly braces of second loop.
    4. Start third loop with a variable name lets say "c" from 5 to greater then a and decrement it by 1 and this loop should also be the nested of first loop but not of second loop.
    5. Write starting curly braces of third loop and then write output statement which prints * like ....................print("*"); ( note: don't write println here) and then write ending curly braces of third loop.
    6. Write statement for line break outside the body of second loop and third loop but inside the body of first loop like ...................println(); and then write ending curly braces of the first loop.

    Saturday, 28 January 2012

    Welcome!

    This is a blog of the java programming and all the tritips ( tricks & tips ) will be uploaded here. Enjoy it!!!