Create a Complex variable named c1 representing 2 j7 Create

Create a Complex variable named c1 representing 2 + j7

Create a Complex variable named c2 representing 5 + j8

Create a Complex variable named c3 and make it equal to c1+c2 by calling the addTwoComplexNumbers function

I need to call a complex function name addTwoComplexNumbers to get c3

Solution

/******************************Complex.java*******************************/

public class Complex {
   // instance variable declaration
   int real, imag;

   // default constructor
   Complex() {
   }

   // parameterized constructor
   Complex(int real, int imag) {
       this.real = real;
       this.imag = imag;
   }

   // This method add two complex number
   Complex addTwoComplexNumbers(Complex C1, Complex C2) {
       Complex sum = new Complex();
       sum.real = C1.real + C2.real;
       sum.imag = C1.imag + C2.imag;
       return sum;
   }
}

/***************************ComplexTest.java**********************/

public class ComplexTest {

   public static void main(String[] a) {
       // creating instance of complex class
       Complex C1 = new Complex(2, 7);
       Complex C2 = new Complex(5, 8);

       Complex C3 = new Complex();
       // calling add method
       C3 = C3.addTwoComplexNumbers(C1, C2);
       // printing result
       System.out.println(\"SUM:\" + C3.real + \"+i\" + C3.imag);
   }
}

/**********************output***************************/

SUM:7+i15

Thanks a lot

Create a Complex variable named c1 representing 2 + j7 Create a Complex variable named c2 representing 5 + j8 Create a Complex variable named c3 and make it equ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site