Programming Homework Help
Programming Homework Help. Write a complex number class in java
Write a complex number class. It will have a default constructor, explicit constructor, and the following methods: read() public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(comlpex) and a toString( method. Include get and set methods as well. Your class should have at least 2 constructors: a default constructor, and an explicit constructor with two arguments of type double.
Please send me a message if you can do this, our professor said this is a really hard problem. So for you pros out there it will probably take like 20 mins.
Here is a demo program that shows what the output should look like and more info about the arithmetic of complex numbers
public class ComplexNumberDemo
{
public static void main (String[] args)
{
ComplexNumber cn1 = new ComplexNumber (4, 5);
ComplexNumber cn2 = new ComplexNumber (3, -2);
ComplexNumber cn3, cn4, cn5, cn6, cn7;
System.out.println (“First ComplexNumber number: ” + cn1);
System.out.println (“Second ComplexNumber number: ” + cn2);
if (cn1.equals(cn2))
System.out.println (“cn1 and cn2 are equal.”);
else
System.out.println (“cn1 and cn2 are NOT equal.”);
cn4 = cn1.add(cn2);
cn5 = cn1.subtract(cn2);
cn6 = cn1.multiply(cn2);
cn7 = cn1.divide(cn2);
System.out.println (“cn1 + cn2: ” + cn4);
System.out.println (“cn1 – cn2: ” + cn5);
System.out.println (“cn1 * cn2: ” + cn6);
System.out.println (“cn1 / cn2: ” + cn7);
}
}
/*
Here are some examples to test your program:
If c1 = 4 + 5i and c2 = 3 -2i then
the sum of c1 and c2 is 7.0 + 3.0i
subtracting c2 from c1 is 1.0 + 7.0i
multiplying c1 and c2 is 22.0 + 7.0i
dividing c1 by c2 is 0.1538461538 + 1.7692307692i
Complex (Imaginary) Number Arithmetic
Complex Numbers are represented by a “real part”, and an
“imaginary part”. The imaginary part is not real because it
is multiplied by √(-1). The √(-1) is represented by the
letter „i‟.
A complex number is written in the form:
(a + b*i)
(a)= the real part
(b)= the imaginary part
(i)= √(-1)
In this document we will write the second imaginary number
as:
(c + d*i)
(c)= the real part
(d)= the imaginary part
(i)= √(-1)
In a complex number class the real and imaginary parts are
stored as double‟s, and (i) is not stored because it‟s just
assumed that the imaginary part is multiplied by √(-1).
When you print out the imaginary number you can output “i”
or something like that after the imaginary part to signify
that it‟s the imaginary part.
Here are some examples of imaginary numbers so you can see
what is the real part and imaginary part.
(1) 1.5+3.2i, real_part=1.5, imaginary_part=3.2
(2) -15, real_part=-15, imaginary_part=0
this is a real number, so to represent it in
complex form, it‟s -15+0i
(3) -8.99i, real_part=0, imaginary_part=-8.99
(this is a complex number with no real part, so
it could be written as: 0-8.99i
(4) -6.2-14.3i, real_part=-6.2, imaginary_part=-14.3
Now that you know how complex numbers break down into two
floating-point number for the real part and the complex
part, we can use the (a)+(b)i notation to demonstrate the
arithmetic that a complex number class should do.Addition:
(a + b*i)+(c + d*i)=(a+c)+(b+d)i
result: real_part=(a+c), imaginary_part=(b+d)
Subtraction:
(a + b*i)-(c + d*i)=(a-c)+(b-d)i
result: real_part=(a-c), imaginary_part=(b-d)
Multiplication:
(a + b*i)*(c + d*i)=(a*c-b*d)+(a*d+b*c)i
result: real_part=(a*c-b*d), imaginary_part=(a*d+b*c)
Division:
(a + b*i)/(c + d*i)
= ((a*c+b*d)/(c2+d2)) + ((b*c-a*d)/(c2+d2))i
result: real_part=((a*c+b*d)/(c2+d2))
imaginary_part=((b*c-a*d)/(c2+d2))
Equality:
(a + b*i) = (c + d*i) if a=c, and b=d
Greater than, and less than do not make sense with complex
numbers because the real part could be greater, and the
imaginary part could be less or vice-versa.
Here are some examples to test your program:
If c1 = 4 + 5i and c2 = 3 -2i then
the sum of c1 and c2 is 7.0 + 3.0i
subtracting c2 from c1 is 1.0 + 7.0i
multiplying c1 and c2 is 22.0 + 7.0i
dividing c1 by c2 is 0.1538461538 + 1.7692307692i