Saturday, June 8, 2013

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)

0 comments:

Post a Comment

    Translate

    Protected by Copyscape