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.

    Translate

    Protected by Copyscape