Friday, 14 September 2012

JAVA Programming: Its easy and also fun

Java is a programming language which has been developed by Sun Micro System. It is obviously an object oriented programming language. Now the question arises “what does an object and object oriented programming mean?” Well I’ll tell you.
Everything in this world is an object. Objects are classified into specific classes. For example:” Blackberry Curve” is an object which belongs to the class “mobile phone”. Objects have their own characteristics:
·         Identity:
An object has its own identity. It means that two objects are distinct even they have the same attributes.
·         Classification:
It means that objects with same attribute and behavior are grouped together in a class.
·         Encapsulation:
Binding and wrapping up the methods (method is a specific implementation of operation by a certain class) and the data (on which the method is applied) together.
·         Inheritance:
        Inheritance is a process by which an object acquires the properties of other object.
·         Polymorphism:
It means that the same function or operation behave differently on different classes.
 Object oriented programming (OOP) involves representing object in programming language and using them.  Software is a collection of discreet objects.
Now, it’s clear that a class is a collection of objects of same attribute (size, name, type) and behavior (function). 
JAVA byte code:
Java byte code is a machine instruction for the JAVA processor chip called the JAVA virtual machine.Java byte code is independent of the of the computer system it has to run upon.
JAVA Virtual Machine(JVM):
 To run a program written  on another system,its byte code needs to be converted into its own JAVA Virtual Machine. JVM is interpreter which translates the program to its own machine language to run it and give the result.
 Class Declaration:
  • Write the access specifier.
  •  Write the keyword "class".
  • Write the class name.
  • Begin the class by opening curly brackets.
  • Write the data members and member functions.
  • End the class by closing curly brackets.
Syntax:
public class Program1
{
     data members;
     member functions;
}
Now let us do some programming in JAVA. I’ve given u their source codes, if you have Blue J installed in your computer you may try them out.
Have you ever thought of making a calculator yourself? Well, probably not. Here is the source code which will help you to make your own simple calculator  with four functions.. i.e. , addition, subtraction ,multiplication and division:
         import java.io.*;
class calculator
{
    public void main()throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = 0, b = 0,ch = 0;
        System.out.println("enter two numbers");
        a = Integer.parseInt(br.readLine());
        b = Integer.parseInt(br.readLine());
        System.out.println("MENU");
        System.out.println("Press 1 for addition");
        System.out.println("Press 2 for subtraction");
        System.out.println("Press 3 for multiplication");
        System.out.println("Press 4 for division");
        ch = Integer.parseInt(br.readLine());
        switch (ch)
        {
            case 1:
                   int s = 0;
                   s = a + b;
                   System.out.println("The sum is "+s);
                   break;
           case 2:
                  int d = 0;
                  d = a - b;
                  System.out.println("The Difference is "+d);
                  break;
           case 3:
                  int m = 0;
                  m = a * b;
                  System.out.println("The Product is "+m);
                  break;
           case 4 :
                   double r = 0;
                   r = a / b;
                   System.out.println("The quotient is "+r);
                   break;
           default:
                  System.out.println("Invalid Input");
                  break;
        }//end of switch
    }//end of main()
}//end of class
Now let us find what are the numbers between 100 and 1000 in which the sum of the cube of the digits of the number is the number itself i.e., it’s an Armstrong number. Here is its source code for you to try..
Import  java.io.*;
class  Armstrongno2
{
    public void main()throws IOException
    {
        int i = 0,d1 = 0,d2 = 0,d3 = 0,a= 0;
        for(i= 100;i<=999;i++)
        {           
            d1 = i%10;
            d2 = ((i/10)%10);
            d3 = i/100;
            a = (d1*d1*d1 + d2*d2*d2 + d3*d3*d3);
            if(a==i)
              System.out.println(a);
          }//end of for
    }//end of main()
}//end of class
I shall give you some more source codes of JAVA programming in Blue J in my next blog. Hope these source codes work. If you have any problem, you may contact me. Here is my email:deswarnava@gmail.com. Or you may send me a friend request in facebook. My link in facebook is https://www.facebook.com/#!/swarnava.de.5