Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Tuesday, 14 June 2011

How can you compile and execute your java program


After you have installed java open your command prompt or simply click at start button and then click run.
A small window will be opened. Write cmd in the text/search box and press run button.

Once you have opened command prompt open the drive/partition in which you have saved your program.
For example i have save my program in "local disk d/my progs". Write "d:" in command prompt and press enter.

After this write the folder name (as in above paragraph d/my progs) and press enter. I will write it as "my progs" and then enter. Now, write your program name and press enter. Just like this:

javac salary.java

there "javac" is the command to compile your program and "salary.java" shows that the programs name is salary and it is a java file.

Finally your program is compiled and ready to execute. When your program will be compiled successfully a class file of the program will be created in the same folder as in your program. To execute your program just write the following command

java salary

and your program will be executed.

Note: 
        "salary" is the program name. You should write the name of your program.

Arithmatic operators and their uses


Operators
Use
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
++
Increment
--
Decrement
=
Assignment
+=
Addition assignment
-=
Subtraction assignment
*=
Multiplication assignment
/=
Division assignment
%=
Modulus assignment




    Monday, 13 June 2011

    Using different arithmatic assignment operators in java program


    class assignops
    {
    public static void main (String arg[])
    {
    int a=10,b=25,c=5,d=18,e=49;
    a+=10;
    b-=10;
    c*=10;
    d/=2;
    e%=7;
    System.out.println ("\n\t a = "+a);

    System.out.println ("\n\t b = "+b);
    System.out.println ("\n\t c = "+c);

    System.out.println ("\n\t d = "+d);
    System.out.println ("\n\t e = "+e);
    }
    }

    Note:
              After execution you will observe following five changes
    1. Value of 'a' is increased by 10
    2. Value of 'b' is decreased by 10
    3. Value of 'c' is 50 (due to multiplying by 10)
    4. Value of 'd' is 9 (because we are dividing 'c' by 2)
    5. Value of 'e' is 0 (because 7 divides 49 properly)
     

      Using psot/prefix decrement operator in java program


      class apdecrement
      {
      public static void main (String arg[])
      {
      int a=97,b=23;
      int c,d;
      c=--a;
      d=b--;
      System.out.println ("\n\t a = "+a);
      System.out.println ("\n\t b = "+b);
      System.out.println ("\n\t c = "+c);
      System.out.println ("\n\t d = "+d);
      }
      }


      Note:
                After executing this program you will see the decrement in the values of 'a' , 'b' and 'c' but value of 'd' will as it is. Just like post fix increment increases value after it is used, post fix decrement also do so but it decreases the value by 1 after it is used.

      Using post/prefix increment operator in java program


      class apincrement
      {
      public static void main (String arg[])
      {
      int a=15,b=34;
      int d,e;
      d=++a;
      e=b++;
      System.out.println ("\n\t a = "+a);
      System.out.println ("\n\t b = "+b);
      System.out.println ("\n\t d = "+d);
      System.out.println ("\n\t e = "+e);
      }
      }


      Note:
               When you will execute this program you will see that values of 'a' , 'b' and 'd' are increased by 1 but value of 'e' is same as we initialized it by 34. The reason behind this is we are using post fix increment operator. In case of post fix increment value of the variable is used first and then it is increased by 1.

       

      Using modulus or '%' operator in java program


      class apmodulus
      {
      public static void main (String arg[])
      {
      int a=15,b;
      b=a%5;
      if (b==0)
      System.out.println ("\n\t Divisible by 5 ");
      else
      System.out.println ("\n\t Not divisible by 5 ");
      }
      }

      Note:
                Modulus operator returns remainder. In the above program variable 'a' will be divided by 5. If 5 divides 'a' properly (with out returning any decimal answer) then the message "Divisible by 5" will be showed otherwise statement associated with 'else' will be executed.

      Using switch statement in java

      import java.util.*;
      class apswitch
      {
      public static void main (String arg[])
      {
      Scanner in = new Scanner (System.in);
      int a;
      System.out.print ("\n\t Enter the number = ");
      a=in.nextInt();
      switch (a)
      {
      case 1:
      System.out.println ("\n\t One ");
      break;
      case 2:
      System.out.println ("\n\t Two");
      break;
      case 3:
      System.out.println ("\n\t Three");
      break;
      case 4:
      System.out.println ("\n\t Four ");
      break;
      case 5:
      System.out.println ("\n\t Five ");
      break;
      default:
      System.out.println ("\n\t Out of range ");
      }

      }

      Sunday, 12 June 2011

      Control/decision making statements of java

      Java supports two control statements:

      • If statement 
      • Switch statement
      You can make easily with the help of these statements Syntax of "if" statement is:

      if (condition)
      statement;

      Here condition is a Boolean expression. If the condition is true then the statement right after the "if" statement will be executed otherwise java compiler will bypass that statement and move to the other statements.

      Syntax of switch:

      switch (variable name)
      {
      case 1:
      statement 1;
      .
      .
      .
      statement n;
      break;
      case 2:
      statement 1;
      .
      .
      .
      statement n;
      break;
      .
      .
      .
      .
      case n;
      statement(s);
      break;
      default:
      statement;
      }

      Here "variable name" shows the variable whom you want to compare with the values defined by "case(s)". If you are comparing numeric values then simply write them after writing "case" and then colon. Once you have done, write your statement(s) you want to execute. You must have noticed the "break" statement. It is used to get out of the "switch" body and prevents other statements to be executed directly.

      Another new statement "default" is used here. It is used at the end of the switch body. Suppose if all the cases you defined above do not mach with the value of the variable you are comparing then the statement right after "default" will be executed.

      To see a program click here!