1.In class Complex, define a multiply method that returns the product of two Com
ID: 3801165 • Letter: 1
Question
1.In class Complex, define a multiply method that returns the product of two
Complex numbers. Suppose you are trying to compute the product of two
complex numbers a + bi and c + di. The real part will be ac –
bd, while the imaginary part will be ad + bc. Modify ComplexTest to
test your solution.
2.Overload the add, subtract and multiply methods of your class, so that
instead of taking one complex as an argument and returning a complex,
they take two complex number as arguments and return the result in a new
complex object. Make this methods static.
Explanation / Answer
package chegg;
public class Complex {
int real,imag;
Complex(){}
Complex(int real_1,int imag_1)
{
real=real_1;
imag=imag_1;
}
static Complex multiply(Complex C1,Complex C2)
{
Complex CSum=new Complex();
CSum.real=C1.real*C2.real-C1.imag*C2.imag ;
CSum.imag=C1.real*C2.imag + C2.real*C1.imag;
return CSum;
}
public static void main(String[] a)
{
Complex C1=new Complex(4,6);
Complex C2=new Complex(5,7);
Complex r=new Complex();
r=Complex.multiply(C1,C2);
System.out.println("Complex Number one is: " + C1.real +"+i" + C1.imag);
System.out.println("Complex Number two is: " + C2.real +"+i" + C2.imag);
System.out.println("Product of Two Complex Number is:" + r.real +"+i" + r.imag);
}
}
---------------------------------------------------
Output Sample 1:-
Complex Number one is: 4+i6
Complex Number two is: 5+i7
Product of Two Complex Number is:-22+i58
------------------------------
Output Sample 2:-
Complex Number one is: 4+i6
Complex Number two is: 1+i3
Product of Two Complex Number is:-14+i18
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.