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

Saturday, January 29, 2011

Create a simple console calculator using shell scripts

This coding gives you a some idea about, how to use loops and if else statements and other simple things. I think this coding will be more valuable for beginners.
You can use editors that support graphical interface and you can detect your errors very easily. 
Application -> Accessories -> Text Editors
First you have to save your script using .sh extension.   
                ex :-  cal.sh
After that you can type the coding......


#This is a simple calculator


option=-1
num1=0
num2=0


echo This is my calculator
echo Availabale operations
echo -e "\tAddition\t\t - 0"
echo -e "\tSubstraction\t\t - 1"
echo -e "\tMultiplication\t\t - 2"
echo -e "\tDivition\t\t - 3"
echo -e "\tFactorial\t\t - 4"
echo -e "\tAny power of any base\t - 5"


echo -n "What is the operation that you want  -  "
read num


if [ $num -eq 0 ]
then 
        echo -n "Enter the number 1 - "
        read num1
        echo -n "Enter the number 2 - "
        read num2
        echo Answer is `expr $num1 + $num2`
else
        if [ $num -eq 1 ]
        then
                  echo -n "Enter the number 1 - "
                  read num1   
                  echo -n "Enter the number 2 - "
                  read num2
                 echo Answer is `expr $num1 - $num2
        else
                 if [ $num -eq 2 ]
                 then       
                      echo -n "Enter the number 1 - "
                         read num1
                         echo -n "Enter the number 2 - "
                         read num2
                         echo Answer is `expr $num1 \* $num2`
                 else
                    if [ $num -eq 3 ]
                    then 
                                         echo -n "Enter the number 1 - "
                                    read num1
                                    echo -n "Enter the number 2 - "
                                    read num2
                                    echo Answer is `expr $num1 \/ $num2`
                                 echo Remainder is `expr $num1 \% $num2`
                    else     
                                        if [ $num -eq 4 ]
                                then
                                            echo -n "Enter the number - "
                                            read num1
                                            answer=1
                                            while [ $num1 -gt 0 ]
                                             do
                                              answer=`expr $answer \* $num1`
                                              num1=`expr $num1 - 1`
                                            done
                                            echo "Answer is " $answer
                                else
                                   if [ $num -eq 5 ]
                                   then
                                               echo -n "Enter the Base - "
                                               read num1
                                               echo -n "Enter the power - "
                                               read num2
                                               answer=1
                                                       while [ $num2 -gt 0 ]
                                                        do
                                                          answer=`expr $answer \* $num1`
                                                          num2=`expr $num2 - 1`
                                                       done
                                                       echo "Answer is " $answer
                                    fi 
                                fi
                       fi
                 fi
         fi
fi

After finish the coding you have to execute this script. To execute you have to open the terminal first. 
Then type sh<space><script name>
          ex :- sh cal.sh
Then press Enter and see the out put

java VS .NET

How to create your own web browser using visual studio C#

1. As usually first you have to create a windows form application
2. Then change your form name and change "IsMdiContainer" property to "True"










3. Then drag and drop a web browser and a tool Strip from the web browser.




4. Then Add buttons and labels and text boxes to your tool strip.
   you can use any amount of buttons to go to your preferable sites by pressing one button.
  you  can set images and sizes to your buttons.
5.After that you have to write codings for every butoon click event (double click on the button).
  Add below codings for that events.


Next button 
webBrowser1.GoForward();


Previous button
webBrowser1.GoBack();


Go button
webBrowser1.Navigate(new Uri("http://"+toolStripTextBox1.Text));


Facebook shortcut button
webBrowser1.Navigate(new Uri("http://www.facebook.com/"));


Gmail shortcut button
webBrowser1.Navigate(new Uri("http://www.gmail.com/"));


  After all these, coding will be view like below. 
6. You can run and check your own web browser.



Wednesday, January 26, 2011

How to create a simple media player using Visual Studio C#

As usually you have to create a windows form application in visual studio
File->New->Project


Then you have to select c# windows form application and give the name and location for your project. After that press ok.


Then Right-click on the toolbox and press "choose Items". Then the following window will appear.


Press "COM component" tab.
Then put a tick on windows media player according to the following picture and press OK.


Now you can see there is tool "Windows media player" on the bottom of the toolbox.


Then drag and drop and change the size as you wish.


After that drag and drop a Menu Strip from the toolbox and give the first one as "Open".


Then Double-click on "Open".



Write a coding as it is in the following picture.



                    OpenFileDialog open = new OpenFileDialog();
                    open.Title = "Open";
                    open.Filter = "All Files|*.*";
                    try
                    {
            
                              if(open.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                                                      axWindowsMediaPlayer1.URL=(open.FileName);
                    }
                    catch (Exception ex)
                    {
                             MessageBox.Show(ex.Message.ToString(),"Error", MessageBoxButtons.OK,       MessageBoxIcon.Error);                
                    }

At last run the program and watch a movie using your own media player.

Sunday, January 23, 2011

How to print the content of any text file?

Write a program to read lines from a text file and display the content to the user.

Hint:
         You may have to use FileInputStream, InputStreamReader and
         BufferedReader classes.
         When you ends the reading readLine() method will give you null values

         FileInputStream fs = new FileInputStream("tutorial.txt");
         BufferedReader in = new BufferedReader(new InputStreamReader(fs));


Answer :  "tutorial.txt" is my file name. you have to use a existing file name in your computer. if it is not in the same folder that you save this java programe, you have to give the full path of that file.


import java.io.*;


class File{
public static void main(String args[]) throws IOException { 
FileInputStream fs = new FileInputStream("tutorial.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(fs));
String text=in.readLine();


while(text!=null){
System.out.println(text);
text=in.readLine();
}
  }
}

Saturday, January 22, 2011

How to get user inputs using keyboard?

Tutorial


01. Write a Java program to convert a given temperature in Celsius to Fahrenheit or to
convert  a  given  temperature  in  Fahrenheit  to  Celsius.  Get  all  the  inputs  as
keyboard  inputs.  Your  program  should  have  two  methods
           celsiusToFahrenheit()
                       and
            fahrenheitToCelsius()
to  perform  the  operations  mentioned  above.    When
ever you run the program main method should display  an output as given below.
Enter
          1 – To convert Celsius to Fahrenheit
          2 - To convert Fahrenheit to Celsius
         Any other number – To exit

        *  When the use enters a value 1 or 2, get the temperature in Celsius or in
             Fahrenheit convert to the other form and display.

        * Then you  must display the menu given above again.

        * You exit the program wh en the user  enter any number other than 1 or 2.

        * You may use loop and switch statements.


              Tc = (5/9)*(Tf-32);
              Tf = (9/5)*Tc+32;

Answer :



import java.util.Scanner;


class Temp{
public static void main(String args[]){
System.out.println("Enter\n1 - To convert Celsius to Fahrenheit\n2 - To convert Fahrenheit   to Celsius\nAny other number - To exit");

Scanner sc1 = new Scanner(System.in);
int option = sc1.nextInt();


switch(option){
case 1 : celsiusToFahrenheit();
break;
case 2 : fahrenheitToCelsius();
break;
default : System.exit(0);
}
}
public static void celsiusToFahrenheit(){
System.out.println("Enter celsius value - ");


Scanner sc2 = new Scanner(System.in);
double temp = sc2.nextDouble();

                          System.out.print("Fahrenheit value - "+((9.0/5.0)*temp+32));
}
public static void fahrenheitToCelsius(){
System.out.println("Enter fahrenheit value - ");


Scanner sc2 = new Scanner(System.in);
double temp = sc2.nextDouble();

                          System.out.print("Celsius value - "+((5.0/9.0)*(temp-32)));
}
}




02. Write a Java program to calculate the numb er of digits and number of letters in a
word. Get the word as a  command line argument.
Note:
              You may use isDigit() and isLetter() methods in Character class.

Answer :



import java.io.*;


class Text{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the text : ");
String text = br.readLine();

int digits=0, letters=0;
for(int i=0; i<text.length(); i++){
if(Character.isDigit(text.charAt(i)))
digits++;
else if(Character.isLetter(text.charAt(i)))
letters++;
}
System.out.println("Number of letter - "+digits);
System.out.println("Number of digits - "+letters);
}
}

Friday, January 21, 2011

How to Install Java to windows 7

Use this link to get information about Java installation........
http://guruparan.blog.com/2011/01/17/configuring-java-development-kit-in-windows-7/

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();
}
}
}