One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Friday, January 21, 2011

Java Tutorials

01. Write a Java program to create an array and store following data. You must use a nested for loop and generate the values to store in the array using a mathematical expression.


 100     90    80    70    60    50    40    30    20    10
  90      81    72    63    54    45    36    27    18    9
  80      72    64    56    48    40    32    24    16    8
  70      63    56    49    42    35    28    21    14    7
  60      54    48    42    36    30    24    18    12    6
  50      45    40    35    30    25    20    15    10    5
  40      36    32    28    24    20    16    12    8      4
  30      27    24    21    18    15    12    9      6      3
  20      18    16    14    12    10    8      6      4      2
  10      9      8      7      6     5      4      3      2      1


Answer :  I cannot say this is the best way to write this coding...


class Tutorial{


         public static void main(String args[]){


                 int arry[][] = new int[10][10]; //create a 2d array which has 10 col & 10 rows
                 int x = 100, z = x, y = 10;


                 for(int i=0; i<10;i++) {        //Input elements to the array
                         x=z;

                         for(int j=0; j<10; j++) {
                                  arry[i][j]=x;
                                   x=x-y;
                         }
                         y=y-1;
                         z-=10;
                 }
                 for(int i=0; i<10; i++) {          //Display the array elements
                           for(int j=0; j<10; j++) {
                                      System.out.print(arry[i][j]+"\t");  // "\t" use to set tabs
                           }
                          System.out.println();
                 }
}


02. Write a following statement  using Conditional (? :)


    if(service < 20)
              if(service < 5)
                         gratitude = 50000;
              else
                         gratitude = service*2500 + 50000;
    else
              gratitude = service*2500 + 100000;


Answer :


    gratitude = (service < 20)?((service < 5)?50000:service*2500+50000):service*2500+100000;




03. Write a program to generate two integer random numbers between 0 and 100. Then print them in sorted manner. (Math.random() method generates random numbers between 0 and 1).


Answer :


class Tutorial{
              public static void main(String args[]){


                       int num1=(int)(Math.random()*100);
                       int num2=(int)(Math.random()*100);

                       if(num1<num2)
                                    System.out.println(num1+" , "+num2);
                       else
                                    System.out.println(num2+" , "+num1);
             }
}


04. Write a Java program to convert a given decimal number into binary form.


Answer : You have many alternatives to write this coding. you can do this using inbuilt methods of the covering class or can use a simple loop.


Method 1 :


class Tutorial{
             public static void main(String args[]){
                        int number = 8;


                        System.out.println("Decimal - "+number);
                        System.out.println("Binary  - "+Integer.toBinaryString(number));
             }
}


Method 2 :


class Tutorial{
                 public static void main(String args[]){
                                  int number = 8;
                                  int numberS = number;
                                  String binary="";


                                  do{
                                                     if ((number/2)*2==number){
                                                               number = number / 2;
                                                               binary = "0"+binary;
                                                     }          
                                                     else{
                                                     number = number / 2;
                                                     binary = "1"+binary;
                                                     }
                                   }while(number>=2);


                                   binary = ("1"+binary);


                                   System.out.println("Decimal - "+numberS);
                                   System.out.println("Binary  - "+binary);
                }
}


Method 3 :


class Tutorial{
              public static void main(String args[])
              {
                          int number = 3;
                          System.out.println("Number  -  "+number);
                          String binary = "";

                          while(number>1){
                                          binary = number%2 + binary;
                                          number/=2;
                          }
                          binary = "1" + binary;
                          System.out.println("Binary  -"+binary);
               }
}


05. Write a program to get an output as given below. You must use for loops.


            9       9       9      9      9      9      9      9       9
                8       8       8      8      8      8      8      8
                    7       7      7      7      7      7      7
                        6       6      6      6      6      6
                             5      5      5      5      5
                                4      4     4      4
                                    3      3     3
                                        2    2
                                           1


Answer :


class Tutorial{
public static void main(String args[]){
for(int i=9; i>0; i--){
for(int j=0; j<10-i; j++){
System.out.print(" ");
}
for(int j=0; j<i; j++){
System.out.print(" "+i);
}
System.out.println();
}
}
}

4 comments: