Ex.no:
|
2
|
Complex number manipulation
|
Date :
|
|
Program
classcomplexnumber
{
privateint
a;
privateint
b;
staticintobj_no=1;
publiccomplexnumber(int
a, int b)
{
this.a=a;
this.b=b;
}
public
String getcomplexvalue()
{
if(this.b<0)
{
return
a+""+b+"i";
}
else
{
return
a+"+"+b+"i";
}
}
public
static intobjectno()
{
returnobj_no++;
}
public
static String addition(complexnumber num1,complexnumber num2)
{
int
a1=num1.a+num2.a;
int
b1=num1.b+num2.b;
if(b1<0)
{
return
a1+""+b1+"i";
}
else
{
return
a1+"+"+b1+"i";
}
}
public
static String subtraction(complexnumber num1,complexnumber num2)
{
int
a1=num1.a-num2.a;
int
b1=num1.b-num2.b;
if(b1<0)
{
return
a1+""+b1+"i";
}
else
{
return
a1+"+"+b1+"i";
}
}
public
static String multiplication(complexnumber num1,complexnumber num2)
{
int
a1=num1.a*num2.a;
int
b1=num1.b*num2.b;
int
vi1=num1.a*num2.b;
int
vi2=num2.a*num1.b;
int vi;
vi=vi1+vi2;
if(vi<0)
{
return
a1-b1+""+vi+"i";
}
else
{
return
a1-b1+"+"+vi+"i";
}
}
}
classcomplexdemo
{
public
static void main(String args[])
{
complexnumber
com1=new complexnumber(-2,-3);
System.out.println("the
object number is"+complexnumber.objectno());
System.out.println(com1.getcomplexvalue());
complexnumber
com2=new complexnumber(-4,-5);
System.out.println("the
object number is"+complexnumber.objectno());
System.out.println(com2.getcomplexvalue());
System.out.println("addition
of both complexnumber
are:"+complexnumber.addition(com1,com2));
System.out.println("subtraction
of both complexnumber
are:"+complexnumber.subtraction(com1,com2));
System.out.println("multiplication
of both complexnumber
are:"+complexnumber.multiplication(com1,com2));
}
}
No comments:
Post a Comment