1. A complex number is a number in the form a bi, where a and b are real numbers
ID: 3605056 • Letter: 1
Question
1. A complex number is a number in the form a bi, where a and b are real numbers and i is sqrt(-1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas a+bi + c + di = (a + c) + (b + d)i a+bi-(c + di) = (a-c)+(b-d)i (a bi) (c di) (ac bd) (bc+ ad)i (a bi)/(c di) (ac+ bd)/(c2 d2)(bc-ad)i/(c2+ d2) Design and implement a class named Complex for representing complex numbers and the methods add, subtract, multiply, and divide for performing complex number operations, and override the toString() method to return a string representation for a complex number The toString method returns (abi) as a string. If b is 0, it simply returns a. Implement three constructors: Complex(a, b), Complex(a), and Complex(). Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. You should also implement getRealPart() and getlmaginaryPart() methods for returning the real and imaginary part of the complex number, respectively. Write a test program that prompts the user to enter two complex numbers and displays the result of their addition, subtraction, multiplication, and divisionExplanation / Answer
import java.io.*;
import java.util.*;
class Complex{
double real;
double imag;
Complex()
{
real=0;
imag=0;
}
Complex(double a)
{
real=a;
imag=0;
}
Complex(double a,double b)
{
real=a;
imag=b;
}
double getRealPart()
{
return real;
}
double getImaginaryPart()
{
return imag;
}
Complex addition(Complex c2)
{
Complex c3= new Complex();
c3.real=this.real+c2.real;
c3.imag=this.imag+c2.imag;
return c3;
}
Complex subtraction(Complex c2)
{
Complex c3= new Complex();
c3.real=this.real-c2.real;
c3.imag=this.imag-c2.imag;
return c3;
}
Complex multiplication( Complex a)
{
Complex n= new Complex();
n.real=this.real*a.real-imag*a.imag;
n.imag=this.real*a.imag+imag*a.real;
return n;
}
Complex division(Complex c2)
{
Complex n=new Complex();
double deno=Math.pow(c2.real,2)+Math.pow(c2.imag,2);
n.real=(this.real*c2.real+this.imag*c2.imag)/deno;
n.imag=(this.imag*c2.real-this.real*c2.imag)/deno;
return n;
}
void display()
{
System.out.println(" The number is "+real+" + "+imag+" i");
}
@Override
public String toString() {
return String.format(real +" + "+ imag+" i ");
}
}
public class HelloWorld{
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.println(" Enter Real and Imaginery of First Complex Number");
double a = input.nextDouble();
double b = input.nextDouble();
System.out.println(" Enter Real and Imaginery of Second Complex Number");
double c = input.nextDouble();
double d = input.nextDouble();
Complex c1= new Complex(10,15);
Complex c2= new Complex(20,25);
Complex res=new Complex();
System.out.println(" Addition ");
res= c1.addition(c2);
res.display();
System.out.println(" In String "+res.toString());
System.out.println(" Subtraction ");
res= c1.subtraction(c2);
res.display();
System.out.println(" In String "+res.toString());
System.out.println(" Multiplication ");
res=c1.multiplication(c2);
res.display();
System.out.println(" In String "+res.toString());
System.out.println(" Division ");
res=c1.division(c2);
res.display();
System.out.println(" In String "+res.toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.