Saturday, 17 August 2013

Advance java programming using Java class

Asif.java
---------------------------
import java.util.Scanner;
class Asif
{
public static void main(String args[]){
ops a1=new ops();
Scanner in=new Scanner(System.in);
int ch=0,a,b,c;
do{
a1.println("\n1 for add\n2 for Subs\n3 for multi\n4 for Divide\n5 for factorial\n6 for Power\n7 for Quit");
a1.print("Enter Command:");
ch=in.nextInt();
switch(ch){
case 1:
a1.print("Enter A & B:");
a=in.nextInt();
b=in.nextInt();
c=a1.add(a,b);
a1.println("Result="+c);
break;
case 2:
a1.print("Enter A & B:");
a=in.nextInt();
b=in.nextInt();
c=a1.subs(a,b);
a1.println("Result="+c);
break;
case 3:
a1.print("Enter A & B:");
a=in.nextInt();
b=in.nextInt();
c=a1.mult(a,b);
a1.println("Result="+c);
break;
case 4:
a1.print("Enter A & B:");
a=in.nextInt();
b=in.nextInt();
c=a1.div(a,b);
a1.println("Result="+c);
break;
case 5:
a1.print("Enter A:");
a=in.nextInt();
c=a1.fact(a);
a1.println("Result="+c);
break;
case 6:
a1.print("Enter A & B:");
a=in.nextInt();
b=in.nextInt();
c=a1.pow(a,b);
a1.println("Result="+c);
break;
case 7:
break;
default:
a1.println("Error Command!\n");
}
}
while(ch!=7);
}
}

class ops
{
int c;
void print(String arg){
System.out.print(arg);
}
void println(String arg){
System.out.println(arg);
}
int add(int a,int b){
c=a+b;
return c;
}
int subs(int a,int b){
c=a-b;
return c;
}
int mult(int a,int b){
c=a*b;
return c;
}
int div(int a,int b){
c=a/b;
return c;
}
int fact(int a){
int i;
c=1;
for(i=1; i<=a; i++){
c=c*i;
}
return c;
}
int pow(int a,int b){
int i;
int p=1;
for(i=1; i<=b; i++){
p=p*a;
}
return p;
}
}

Output:
----------------------------

1 for add
2 for Subs
3 for multi
4 for Divide
5 for factorial
6 for Power
7 for Quit
Enter Command:

1 comment: