Monday, January 20, 2014

Java Program to illustrate Arithmetic Operation Using Interface

with 0 Comment
import java.io.*
import java.lang.*;
interface arithmetic
{
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);
int mod(int a,int b);
}

class arith implements arithmetic
{
int a,b,c;
arith(int x,int y)
{
a=x;
b=y;
}
public int add(int a,int b)
{
c=a+b;
return c;
}
public int sub(int a,int b)
{
c=a-b;
return c;
}
public int mul(int a,int b)
{
c=a*b;
return c;
}
public int div(int a,int b)
{
c=a/b;
return c;
}
public int mod(int a,int b)
{
c=a%b;
return c;
}
}

class inter
{
public static void main(String args[])throws IOException
{
DataInputStream d=new DataInputStream(System.in);
int a=0,b=0,c=0;
arith a1=new arith(a,b);
System.out.println();
System.out.println("Arithmetic Operation Using Interface");
System.out.println("-------------------------------");
System.out.println("Enter Two Integer Values");
a=Integer.parseInt(d.readLine());
b=Integer.parseInt(d.readLine());
System.out.println("Menu");
System.out.println("-----");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println("5.Modulo Division");
System.out.println("Enter your Choice");
int ch=Integer.parseInt(d.readLine());
switch(ch)
{
case 1:
System.out.println("Addition");
System.out.println("-------");
c=a1.add(a,b);
break;
case 2:
System.out.println("Subtraction");
System.out.println("----------");
c=a1.sub(a,b);
break;
case 3:
System.out.println("Multiplication");
System.out.println("-----------");
c=a1.mul(a,b);
break;
case 4:
System.out.println("Division");
System.out.println("----------");
c=a1.div(a,b);
break;
case 5:
System.out.println("Modulo Division");
System.out.println("--------------");
c=a1.mod(a,b);
break;
}
System.out.println("Result:"+c);
}
}

-------------
OUTPUT:
-------------
Arithmetic Operation Using Interface
---------------------------------------------
Enter Two Interface Values
84
3

Menu
-------
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulo Division

Enter your choice
1

Addition
-----------
Result:87

Enter Two Integer Values
84
3

Enter your choice
2

Subtraction
--------------
Result:81

Enter Two Integer Values
84
3

Enter your choice
3

Multiplication
-----------------
Result:252

Enter Two Integer Values
84
3
Enter your choice
4

Division
----------
Result:28

Enter Two Integer Values
84
5

Enter your choice
5

Modulo Division
---------------------
Result:4

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive