Skip to content

Commit 8fff37f

Browse files
author
java-tester-x
committed
Finished class to work with complex numbers
Finished class to work with complex numbers
1 parent 3ca6fc5 commit 8fff37f

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/MyComplex.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public boolean isImaginary() {
4040
}
4141

4242
public boolean equals(double real, double imag) {
43-
return (thix.real == real && this.imag = imag);
43+
return (this.real == real && this.imag == imag);
4444
}
4545

4646
public boolean equals(MyComplex another) {
47-
return (thix.real == another.getReal() && this.imag = another.getImag());
47+
return (real == another.getReal() && imag == another.getImag());
4848
}
4949

5050
public double magnitude() {
@@ -56,7 +56,7 @@ public double argumentInRadians() {
5656
}
5757

5858
public int argumentInDegrees() {
59-
59+
return 0;
6060
}
6161

6262
public MyComplex conjugate() {
@@ -73,18 +73,23 @@ public MyComplex substract(MyComplex another) {
7373

7474
public MyComplex multiplyWith(MyComplex another) {
7575
// (ac - bd) + (ad + bc)i
76-
return new MyComplex(
77-
real*another.getReal() - imag*another.getImag()
78-
, real*another.getImag() + imag*another.getReal()
79-
);
76+
real = real*another.getReal() - imag*another.getImag();
77+
imag = real*another.getImag() + imag*another.getReal();
78+
return this;
8079
}
8180

8281
public MyComplex divideBy(MyComplex another) {
8382
// [(a + bi) * (c – di)] / (c2 + d2)
84-
83+
MyComplex tmp = multiplyWith(another.conjugate());
84+
double delimiter = another.getReal()*another.getReal() + another.getImag()*another.getImag();
85+
if (delimiter != 0) {
86+
real = tmp.getReal() / delimiter;
87+
imag = tmp.getImag() / delimiter;
88+
}
89+
return this;
8590
}
8691

8792
public String toString() {
88-
return "("+real " + "+ imag+"i)" ;
93+
return "("+real +" + "+ imag+"i)" ;
8994
}
9095
}

src/TestMyComplex.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
package src;
22

3-
public class MyComplex {
3+
public class TestMyComplex {
44

55
public static void main(String[] args)
66
{
7-
7+
MyComplex c1 = new MyComplex(1.1, 2.2);
8+
System.out.println("Number 1 is "+ c1);
9+
System.out.println(c1 + " is "+(c1.isReal() ? "" : "NOT")+" pure real number");
10+
System.out.println(c1 + " is "+(c1.isImaginary() ? "" : "NOT")+" pure imaginary number");
11+
12+
MyComplex c2 = new MyComplex(3.3, 4.4);
13+
System.out.println("Number 2 is "+ c2);
14+
System.out.println(c2 + " is "+(c2.isReal() ? "" : "NOT")+" pure real number");
15+
System.out.println(c2 + " is "+(c2.isImaginary() ? "" : "NOT")+" pure imaginary number");
16+
17+
System.out.println(c1+" is " +(c1.equals(c2) ? "":"NOT") +" equal to "+c2);
18+
System.out.println(c1+" + "+c2+" = "+c1.add(c2));
19+
System.out.println(c1+" - "+c2+" = "+c1.substract(c2));
820
}
921
}

0 commit comments

Comments
 (0)