Write a program to perform addition, subtraction, multiplication in complex numbers using constructors.
Program Code:
import java.io.*;
public class ComplexNumber
{
private int a;
private int b;
public ComplexNumber()
{
}
public ComplexNumber(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 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 substraction(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";}
}
public static void main(String args[])
{
ComplexNumber com1 = new ComplexNumber(-2,-3);
ComplexNumber com2 = new ComplexNumber(-4,-5);
System.out.println(com1.getComplexValue());
System.out.println(com2.getComplexValue());
System.out.println("Addition of both Complex Numbers are :" +ComplexNumber.addition(com1,com2));
System.out.println("Substraction of both Complex Numbers are :" +ComplexNumber.substraction(com1,com2));
System.out.println("Multiplication of both Complex Numbers are :" +ComplexNumber.multiplication(com1,com2));
}
}
Post A Comment:
0 comments: