Displaying complex numbers in java
My goal today is to be able to add, subtract, multiply and divide complex numbers (a + bi) and somehow display this data. I knew that the complexity would arise when I tried to create a method that divides complex numbers. It should display approximately 0.39189-.1486i if my math is correct or ((29-11i) / 74). The output I get is -0.041666666666666664 + 0.4583333333333333i which is not correct.
Can you help me find the error in the ComplexDiv method?
Here is the code:
public String ComplexDiv(ComplexNumbers other) {
String r = Double.toString(((real*other.real)-(complex*other.complex))
/ ((other.real * other.real)-(other.complex * other.complex)));
String c = Double.toString((-(real*other.complex)+(other.real*complex))
/ ((other.real * other.real)-(other.complex * other.complex)));
return r + "+" + c + "i";
}
Here is the test class for calling this method:
public class ComplexTest {
public static void main(String[] args) {
ComplexNumbers c1 = new ComplexNumbers();
ComplexNumbers c2 = new ComplexNumbers();
c1.real = 3;
c1.complex = 2;
c2.real = 5;
c2.complex = 7;
System.out.println(c1.ComplexAdd(c2));
System.out.println(c1.ComplexSub(c2));
System.out.println(c1.ComplexMult(c2));
System.out.println(c1.ComplexDiv(c2));
System.out.println(c1.findConjugate());
}
}
Bonus question: Any idea how I could represent my answers as a fraction instead of a decimal?
Here's the entire ComplexNumbers class to give you an overview of my approach:
package exerciseslearningjava;
public class ComplexNumbers {
public double real;
public double complex;
public double realConvert;
public double compConvert;
public String ComplexAdd(ComplexNumbers other) {
String r = Double.toString(real + other.real);
String c = Double.toString(complex + other.complex);
return r + "+" + c + "i";
}
public String ComplexSub(ComplexNumbers other) {
String r = Double.toString(real - other.real);
String c = Double.toString(complex - other.complex);
return r + c + "i";
}
public String ComplexMult(ComplexNumbers other) {
String r = Double.toString((real * other.real) - (complex*other.complex));
String c = Double.toString((real * other.complex)+(complex*other.real));
return r + "+" + c + "i";
}
public String ComplexDiv(ComplexNumbers other) {
String r = Double.toString(((real*other.real)-(complex*other.complex))
/ ((other.real * other.real)-(other.complex * other.complex)));
String c = Double.toString((-(real*other.complex)+(other.real*complex))
/ ((other.real * other.real)-(other.complex * other.complex)));
return r + "+" + c + "i";
}
public String findConjugate() {
String r = Double.toString(real);
String c = Double.toString(complex);
return r + "-" + c + "i";
}
}
source to share
Your denominator:
((other.real * other.real) - (other.complex * other.complex))
Must not be:
((other.real * other.real) + (other.complex * other.complex))
Since you get this by multiplying a complex number by its conjugation:
(a + bi) * (a - bi) = (a * a) - (b * b * i * i)
But since it i * i
is -1, it becomes:
(a + bi) * (a - bi) = (a * a) + (b * b)
Also, as an aside, I have bad feelings in the back of my neck when I see Strings being used to represent numeric values. Why use strings? Why not float or BigDecimal?
source to share