import java.util.*;
class ComplexNumber implements
Cloneable
{
private double rPart,iPart;
public ComplexNumber()//default constructor
{
rPart = 0;
iPart = 0;
}
public ComplexNumber(double rPart,double iPart)//argument
constructor
{
this.rPart = rPart;
this.iPart = iPart;
}
//arithmetic operations on complex Numbers
public ComplexNumber add (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = this.rPart + other.rPart;
result.iPart = this.iPart + other.iPart;
return result;
}
public ComplexNumber subtract (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = this.rPart - other.rPart;
result.iPart = this.iPart - other.iPart;
return result;
}
public ComplexNumber multiply (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = (this.rPart * other.rPart) - (this.iPart *
other.iPart);
result.iPart = (this.rPart * other.iPart) + (this.iPart *
other.rPart);
return result;
}
public ComplexNumber divide (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
// multiply with complex conjugate
result.rPart = ((this.rPart * other.rPart) -(this.iPart *(-1)*
other.iPart))/ ( (other.rPart * other.rPart) +( other.iPart *
(-1)*(-other.iPart)));
result.iPart = ((this.rPart * other.iPart))
+(this.iPart*(other.rPart))/ ( (other.rPart * other.rPart) +(
other.iPart *(-1)* (-other.iPart))) ;
return result;
}
public double absoluteValue()
{
return Math.sqrt(rPart*rPart + iPart*iPart);
}
//get methods
public double getRPart()
{
return rPart;
}
public double getIPart()
{
return iPart;
}
public String toString()
{
return rPart + " + "+ iPart+" i";
}
public Object clone() throws CloneNotSupportedException
{
ComplexNumber objClone = new ComplexNumber();
objClone.setReal(this.rPart);
objClone.setImag(this.iPart);
return objClone;
}
public void setReal(double rPart)
{
this.rPart = rPart;
}
public void setImag(double iPart)
{
this.iPart = iPart;
}
public double compareTo(ComplexNumber other)
{
return this.getRPart() - other.getRPart();
}
}
class ComplexNumberTester
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the first complex number c1 : ");
double rPart = input.nextDouble();
double iPart = input.nextDouble();
ComplexNumber c1 = new ComplexNumber(rPart,iPart);
System.out.println("Enter the second
complex number c2 : ");
rPart = input.nextDouble();
iPart = input.nextDouble();
ComplexNumber c2 = new ComplexNumber(rPart,iPart);
ComplexNumber add = new ComplexNumber();
ComplexNumber sub = new ComplexNumber();
ComplexNumber mult = new ComplexNumber();
ComplexNumber div = new ComplexNumber();
System.out.println("Complex Number c1 :"+c1);
System.out.println("Complex Number c2 :"+c2);
add = c1.add(c2);
System.out.println("\nAddition of c1 and c2 : "+ add);
sub = c1.subtract(c2);
System.out.println("\nSubtraction of c1 and c2 : "+ sub);
mult = c1.multiply(c2);
System.out.println("\nMutiplication of c1 and c2 : "+ mult);
div = c1.divide(c2);
System.out.println("\nDivision of c1 by c2 : "+ div);
System.out.println("Absolute value of
c1 : "+c1.absoluteValue());
}
}
Output:
Success #stdin #stdout 0.07s 2184192KB
Enter the first complex number c1 : 3.5 5.5 Enter the second complex number c2 : -3.5 1 Complex Number c1 :3.5 + 5.5 i Complex Number c2 :-3.5 + 1.0 i Addition of c1 and c2 : 0.0 + 6.5 i Subtraction of c1 and c2 : 7.0 + 4.5 i Mutiplication of c1 and c2 : -17.75 + -15.75 i Division of c1 by c2 : -0.5094339622641509 + 2.047169811320755 i Absolute value of c1 : 6.519202405202649
Do ask if any doubt. Please upvote.
JAVA PROGRAMMING A complex number is a number in the form a + bi, where a...
A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (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...
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)...
How would you draw a UML chart for Intro to Java Liang (10th edition) Chapter 13 #17PE? I am confused if you start with the main application then a separate box for the class. This is the assignment which I have the programming complete but stuck on UML Chart. Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, and abs for performing complexnumber operations, and override toString method for returning a string representation...
2. A complex number can be expressed as a + bi where a and b are real numbers and i is the imaginary unit. The multiplication of two complex numbers is defined as follows: (a+bi)(c+di) = (ac-bd) + (bc+ad)i Define a class which represents a complex number. The only member functions you have to define and implement are those which overload the * and *= symbols.
A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...
Python has the complex class for performing complex number arithmetic. For this assignment, you will design and implement your own Complex class. Note that the complex class in Python is named in lowercase, while our custom Complex class is named with C in uppercase. A complex number is of the form a + bi, where a and b are real numbers and i is √-1. The numbers a and b are known as the real part and the imaginary part...
c++
2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...
C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers. is the imaginary unit with the property of . is called the real part of the complex number and is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...
For the complex number given as: z = a + bi / c+di where i = √−1 is the imaginary unit. The parameters are defined as a = √2, b = 0, c = 0.5 and d = −0.5. (a) Find the real and the imaginary parts of z, and then draw the Argand dia- gram. (Hint: Use the conjugate of the denominator.) 2.5 (b) Based on the Argand diagram, find the distance r of the complex number z from...