Monday 6 August 2012

Design a class for Complex numbers in Java. In addition to methods for basic operations on complex numbers

Hi,

here is a program to implement complex number in JAVA
----------------------------------------------------------

Program:

import java.io.*;

class complex

{

int a,b;

public static int c;

public complex(int x,int y)

{

a=x;b=y;

c++;


}

public static String add(complex n1,complex n2)

{

int a1=n1.a+n2.a;

int b1=n1.b+n2.b;

if(b1<0)

return (a1+" "+b1+"i");

else

return (a1+" "+b1+"i");

}

public static String sub(complex n1,complex n2)

{

int a1=n1.a-n2.a;

int b1=n1.b-n2.b;

if(b1<0)

return (a1+" "+b1+"i");

else

return (a1+" "+b1+"i");

}

public static String mul(complex n1,complex n2)

{


int a1=n1.a*n2.a;

int b1=n1.b*n2.b;

int v1=n1.a*n2.b;

int v2=n2.a*n1.b;

int vi=v1+v2;

if(vi<0)

return(a1-b1+" "+vi+"i");

else

return(a1-b1+"+"+vi+"i");

}

}

class com

{

public static void main(String a[])throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

int x,y;

System.out.println("enter the no for complex1:");

x=Integer.parseInt(in.readLine());


y=Integer.parseInt(in.readLine());

System.out.println("enter the no for complex2:");

int m=Integer.parseInt(in.readLine());

int n=Integer.parseInt(in.readLine());

complex c1=new complex(x,y);

complex c2=new complex(m,n);

System.out.println("addition:"+complex.add(c1,c2));

System.out.println("subtraction:"+complex.sub(c1,c2));

System.out.println("multiplication:"+complex.mul(c1,c2));

System.out.println("count="+complex.c);

}

}


Output:

C:\j2sdk1.4.0\bin>javac com.java

C:\j2sdk1.4.0\bin>java com
enter the no for complex1:
21
12
enter the no for complex2:
34
45
addition:55 57i
subtraction:-13 -33i
multiplication:174+1353i
count=2`

No comments:

Don't You Think this Awesome Post should be shared ??
| Design a class for Complex numbers in Java. In addition to methods for basic operations on complex numbers |
Back To Top Related Posts Plugin for WordPress, Blogger...