Monday, June 10, 2013

Posted in My Programming Skill

Inverting the Values of given Variable (NOT (~) Operator)

SOURCE CODE

class bitnot { 
        public static void main(String args[]) {      // start of main() method.
                int a = 0, b = 5, c1, c2;     // initializing a & b of type integer.
                c1 = ~a; c2 = ~b;         // inverting a & b using "~" operator and storing in c1 & c2 respectively.
                System.out.println("Inverse : "+c1+ " & " +c2);     // printing result
        }         // ending of main() method.
}      // end of the class.



OUTPUT

Inverse : -1 & -6





NOTE:-
          Not ( ~ ) operator assumes -1 to 0 after inversion.
          Hence whenever we invert any number we get incremented number with negative ( - ) sign.

Saturday, June 8, 2013

Posted in My Programming Skill

Arithmatic Operations using class


SOURCE CODE

class Add {       // start of class Add.
void sum(int x, int y) {       // defining method sum() and passing parameter through it.
x+=y;       // Adding variable x and y, and storing result in variable x.
System.out.println("\nAddition is " +x);       // printing result.
}       // end of method sum().
}       // end of class Add.
class Sub {       // start of class Sub.
void subt(int x, int y) {       // defining method subt() and passing parameter through it.
x-=y;       // Subtracting variable y through x, and storing result in variable x.
System.out.println("Subtraction is " +x);       // printing result.
}       // end of method subt().
}       // end of class Subt.
class Mul {       // start of class Mul.
void mult(int x, int y) {       // defining method mult() and passing parameter through it.
x*=y;       // Multiplying variable x and y, and storing result in variable x.
System.out.println("Multiplication is " +x);       // printing result.
}       // end of method mult().
}       // end of class Mul.
class Div {       // start of class Div.
void divi(int x, int y) {       // defining method divi() and passing parameter through it.
x/=y;       // Dividing variable x by y, and storing result in variable x.
System.out.println("Division is " +x);       // printing result.
}       // end of method divi().
}       // end of class Div.
class Mod {       // start of class Mod.
void modu(int x, int y) {       // defining method modu() and passing parameter through it.
x%=y;       // finding modulus of variable x, and storing result in variable x.
System.out.println("Modulus is " +x);       // printing result.
}       // end of method modu().
}       // end of class Mod.
class Arithmatic_Operators {       // start of main class Arithmatic_Operators.
public static void main(String args[]) {       // defining main() method.
int x=13, y=3;       // initializing variable x and y.
Add a = new Add();       // creating object to the class Add.
Sub s = new Sub();       // creating object to the class Sub.
Mul m = new Mul();       // creating object to the class Mul.
Div d = new Div();       // creating object to the class Div.
Mod mo = new Mod();       // creating object to the class Mod.
a.sum(x,y);     // Accessing method of class Add using dot (.) operator.
s.subt(x,y);     // Accessing method of class  Sub using dot (.) operator.
m.mult(x,y);     // Accessing method of class Mul using dot (.) operator.
d.divi(x,y);     // Accessing method of class Div using dot (.) operator.
mo.modu(x,y);     // Accessing method of class Mod using dot (.) operator.
}       // end of main() method.
}       // end of class Arithmatic_Operators.


OUTPUT

Addition is 16
Subtraction is 10
Multiplication is 39
Division is 4
Modulus is 1




NOTE:-

This program clear the "class within class" concept. This program also shows the use of following operator :-
       1) +=    (Addition Assignment)
       2) -=    (Subtraction Assignment)
       3) *=    (Multiplication Assignment)
       4) /=    (Division Assignment)
       5) %=    (Modulus Assignment)

Posted in My Programming Skill

Increment and Decrements the Value of Variable



SOURCE CODE

class IncDec {       // start class IncDec.
public static void main(String args[]) {       // start of main() method.
int a=10, b=15;       // initializing variables.
System.out.println("Original Numbers are a="+a+ " b="+b);       // Printing the original Numbers
a++;       // incrementing variable a.
b--;       // decrementing variable b.
System.out.println("After Increment a="+a+ " After Decrements b="+b);       // printing the results.
}       //end of main() method.
}       // end of class IncDec.



OUTPUT

Original Numbers are a=10 b=15
After Increment a=11 After Decrements b=14






NOTE:-

Above program increment the variable a by 1 and decrements the variable by 1. And then it prints the result.
If you want to increase or decrease the value of variable by more than 1 then you have to use
c = b+2 or c = b+any_Number.
Posted in My Programming Skill

Modulus of a Number


SOURCE CODE

class Modulus {     // start of class Modulus.
public static void main(String args[]) {     // start of main() method.
int a=15, b=4, c;     // Initializing and declaring variables.
c=a%b;     // finding mod of a number a and storing it in variable c.
System.out.println("Result is "+c);     // printing result.
}     // end of main() method.
}     // end class modulus .


OUTPUT

Result is 3




NOTE:-
 
Mod (%) of a number shows the remainder obtained by dividing another number to that number. You can check by assigning different value to the variable b. 
Instead of using
                c = a % b;
              System.out.println("Multiplication is : "+c);
you can use following single line of code

                System.out.println("Multiplication is : "+(a%b));
If you want to use above single line then you don't have to declare the third variable i.e. c.
In another case you can also use
                c = a%5; or c = a % any_number;
so that you don't have to initialize and assign variable b.

Saturday, June 1, 2013

Posted in My Programming Skill

Divide One Number By Another



SOURCE CODE

class Division {
public static void main(String args[]) {        // start of main() method.
int a=10, b=5;  // initializing a & b of type integer. 
double c; // initializing c of type double. 
c = a / b; //dividing a by b using "/" operator and storing in c.
System.out.println("Division is : "+c);   // printing result
} // end of main() method.
} // end of class.



OUTPUT

Division is : 2.0





NOTE:-


Instead of using
                c = a / b;

              System.out.println("Multiplication is : "+c);
you can use following single line of code

                System.out.println("Multiplication is : "+(float)(a/b));
If you want to use above single line then you don't have to declare the third variable i.e. c.
variable at denominator should be greater than 0. if it is not then it will show the error.

Friday, May 31, 2013

Posted in My Programming Skill

Multiplication of Numbers


SOURCE CODE

class Multiplication {
public static void main(String args[]) { // start of main() method.
int a=9, b=12, c;  // initializing a & b of type integer.
c = a * b; //Multiplying a & b using "*" operator and storing in c.
System.out.println("Multiplication is : "+c);  // printing result.
} // end of main() method.
} // end of class.

OUTPUT

Multiplication is : 108




NOTE:-

 Instead of using
                c = a * b;
              System.out.println("Multiplication is : "+c);
you can use following single line of code

                System.out.println("Multiplication is : "+(a*b));
If you want to use above single line then you don't have to declare the third variable i.e. c.
You can multiply multiple numbers at a time.
Posted in My Programming Skill

Subtraction of Numbers


SOURCE CODE

class Subtraction {
public static void main(String args[]) { // start of main() method.
int a=10, b=14, c;  // initializing a & b of type integer.
c = a - b; //subtracting b from a using "-" operator and storing in c.
System.out.println("Subtraction is : "+c);  // printing result
} // end of main() method.
} // end of class.


OUTPUT

Subtraction is : -4




NOTE:-

   Instead of using
                c = a - b;
              System.out.println("Subtraction is : "+c);
you can use following single line of code

                System.out.println("Subtraction is : "+(a-b));
If you want to use above single line instead of those two then you don't have to declare the third variable i.e. c.


Posted in My Programming Skill

Sum Of Two Numbers

SOURCE CODE

class addition {
public static void main(String args[]) { // start of main() method.
    int a=10, b=14, c; // initializing a & b of type integer.
    c = a + b; // adding a & b using "+" operator and storing in c.
    System.out.println("Addition : "+c);     // printing result
}         // ending of main() method.
}      // end of the class.


OUTPUT

Addition : 24






NOTE:-
   Instead of using
                c = a + b;
System.out.println("Addition : "+c);
you can use following single line of code

                System.out.println("Addition : "+(a+b));
If you want to use above single line then you don't need to declare the third variable i.e. c.
Posted in My Programming Skill

Operators in JAVA




Operators are the thing which operates on variable to produce results. They manipulate both the data and variables. JAVA provides wide variety of operators. Those are as follows:-

1)      Arithmetic Operators:-
These operators are used on mathematical expressions. These operators are as follows:-

Sr. No.
Operator
Meaning
Sample Program
1
+
Addition
2
-
Subtraction
3
*
Multiplication
4
/
Division
5
%
Modulus
6
+=
Addition Assignment
7
-=
Subtraction Assignment
8
*=
Multiplication Assignment
9
/=
Division Assignment
10
%=
Modulus Assignment
11
++
Increment
12
--
Decrement

2)      Bitwise Operators:-
These operators are operated on type int, long, short, char and byte. These operators operate on individual bit of operand. These operators are as follows:-

Sr. No.
Operator
Meaning
Sample Program
1
~
Bitwise NOT
2
&
Bitwise AND
Click HERE
3
|
Bitwise OR
Click HERE
4
^
Bitwise EXCLUSIVE OR (XOR)
Click HERE
5
&=
Bitwise AND Assignment
Click HERE
6
|=
Bitwise OR Assignment
Click HERE
7
^=
Bitwise EXCLUSIVE OR (XOR) Assignment
Click HERE
8
>> 
Shift right
Click HERE
9
>>> 
Shift right zero fill
Click HERE
10
>>=
Shift right Assignment
Click HERE
11
>>>=
Shift right zero fill Assignment
Click HERE
12
<< 
Shift left
Click HERE
13
<<=
Shift left Assignment
Click HERE

3)      Relational Operators:-
These operators used to decide the relation between the operands. These operators are as follows:-

Sr. No.
Operator
Meaning
Sample Program
1
==
Equal to
Click HERE
2
!=
Not equal to
Click HERE
3
> 
Greater than
Click HERE
4
>=
Greater than or equal to
Click HERE
5
< 
Less than
Click HERE
6
<=
Less than or equal to
Click HERE


4)      Logical Operators:-
Logical operators are used to combine operands. These operators are as follows:-

Sr. No.
Operator
Meaning
Sample Program
1
&&
Logical AND
Click HERE
2
||
Logical OR
Click HERE
3
!
Logical NOT
Click HERE
4
^
Logical XOR
Click HERE
5
?:
Ternary if - else
Click HERE

5)      Assignment Operators:-
These operators are used to assign a particular value to the operator or to check the value of particular operand.


Sr. No.
Operator
Meaning
Sample Program
1
=
Assign the value
Click HERE
2
==
Check the value
Click HERE
3
===
Check the value with case sensitive
Click HERE


    Translate

    Protected by Copyscape