OOP – ASSIGNMENT XII

Lab Manual Download: Write a program to implement addition, subtraction and multiplication of two complex numbers in Java

PROGRAM

class Complex
{
 int real,img; // real and img variable to store complex no
 //Constructor of Complex class
 Complex()
 {
 real=-1;
 img=-1;
 }
 void read()
 { 
 System.out.println("***********Complex Number**********\nEnter real and imaginary part");
 real=Integer.parseInt(System.console().readLine());
 img=Integer.parseInt(System.console().readLine());

 }
 void display()
 {
 System.out.println("Complex number is:"+real+" + "+img+"i");
 }
 Complex add(Complex c)
 {
 Complex temp=new Complex();
 temp.real=real+c.real;
 temp.img=img+c.img;
 return temp;
 }
 Complex sub(Complex c)
 {
 Complex temp=new Complex();
 temp.real=real-c.real;
 temp.img=img-c.img;
 return temp;

 }
 Complex mul(Complex c)
 {
 Complex temp=new Complex();;
 temp.real=(real*c.real)-(img*c.img);
 temp.img=(real*c.img)+(img*c.real);
 return temp;
 }
 public static void main(String args[])
 {
 Complex c1=new Complex();
 Complex c2=new Complex();
 c1.read(); 
 c2.read(); 
 c1.display();
 c2.display();
 Complex c3=new Complex();
 System.out.println("*********ADDITION************");
 c3=c1.add(c2);
 c3.display();
 System.out.println("*********SUBTRACTION************");
 c3=c1.sub(c2);
 c3.display();
 System.out.println("*********MULTIPLICATION************");
 c3=c1.mul(c2);
 c3.display();
 
 
 }
}

OUTPUT

ass-12-op

Leave a Reply

Your email address will not be published. Required fields are marked *