I have first listed my class code, then my tester/driver code and have put my questions at the bottom. My code for my class is as follows :
class Complex {
//data values
private double real;
private double imag;
//constructors
public Complex () {
real = 0;
imag = 0;
}
public Complex (double realInput) {
real = realInput;
imag = 0;
}
public Complex (double realInput, double imagInput) {
real = realInput;
imag = imagInput;
}
//accessors
public double getReal () {
return real;
}
public double getImag () {
return imag;
}
//modifiers
public void setReal (double inputReal) {
real = inputReal;
}
public void setImag (double inputImag) {
imag = inputImag;
}
//toString method
public String toString() {
return real + " + " + imag + "i";
}
//instance methods
//addition methods
public Complex add (double realInput) {
real = real + realInput;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
public Complex add (Complex complex) {
double firstReal = complex.getReal();
double firstImag = complex.getImag();
double secondReal = this.getReal();
double secondImag = this.getImag();
real = firstReal + secondReal;
imag = firstImag + secondImag;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//subtraction methods
public Complex subtract (double realInput) {
real = real - realInput;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
public Complex subtract (Complex complex) {
double newReal = complex.getReal();
double newImag = complex.getImag();
real = this.getReal() - newReal;
imag = this.getImag() - newImag;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//multiplication methods
public Complex multiply (double realInput) {
real = real * realInput;
imag = imag * realInput;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//****problem code****
public Complex multiply (Complex complex) {
double newReal = complex.getReal();
double newImag = complex.getImag();
real = ((real * newReal) - (imag * newImag));
imag = ((real * newImag) + (imag * newReal));
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//division methods
public Complex divide (double realInput) {
real = real / realInput;
imag = imag / realInput;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//****problem code****
public Complex divide (Complex complex) {
double newReal = complex.getReal();
double newImag = complex.getImag();
real = this.getReal();
imag = this.getImag();
double newRealNumerator = (real * newReal) + (imag * newImag);
double newRealDenominator = (Math.pow(newReal, 2) + Math.pow(newImag, 2));
real = newRealNumerator / newRealDenominator;
double newImagNumerator = (imag * newReal) - (real * newImag);
double newImagDenominator = newRealDenominator;
imag = newImagNumerator / newImagDenominator;
Complex newComplex = new Complex(real, imag);
return newComplex;
}
//equals method
public boolean equals (Complex complex) {
double firstReal = complex.getReal();
double firstImag = complex.getImag();
double secondReal = this.getReal();
double secondImag = this.getImag();
boolean testEquals = false;
if (firstReal == secondReal && firstImag == secondImag) {
testEquals = true;
}
return testEquals;
}
}//end class
My code for my tester/driver is as follows:
class ComplexTester {
public static void main(String[] args ) {
//declaring Complex objects
Complex one = new Complex ();
Complex two = new Complex (3);
Complex three = new Complex (1, 4);
Complex four = new Complex (2, 3);
//testing addition methods
System.out.println("Testing addition methods...");
System.out.println("(" + three.toString() + ") + (" + 3.0 + ") = " + three.add(3.0));
three.setReal(1);
three.setImag(4);
System.out.println("(" + three.toString() + ") + (" + four.toString() + ") = " + three.add(four));
three.setReal(1);
three.setImag(4);
//testing subtraction methods
System.out.println();
System.out.println("Testing subtraction methods...");
System.out.println("(" + three.toString() + ") - (" + 3.0 + ") = " + three.subtract(3.0));
three.setReal(1);
three.setImag(4);
System.out.println("(" + three.toString() + ") - (" + four.toString() + ") = " + three.subtract(four));
three.setReal(1);
three.setImag(4);
//testing multiplication methods
System.out.println();
System.out.println("Testing multiplication methods...");
System.out.println("(" + three.toString() + ") * (" + 3.0 + ") = " + three.multiply(3.0));
three.setReal(1);
three.setImag(4);
System.out.println("(" + three.toString() + ") * (" + four.toString() + ") = " + three.multiply(four));
three.setReal(6);
three.setImag(3);
//testing division method
System.out.println();
System.out.println("Testing division methods...");
System.out.println("(" + three.toString() + ") / (" + 3.0 + ") = " + three.divide(3.0));
three.setReal(4);
three.setImag(2);
Complex testDiv = new Complex(3, -1);
System.out.println("(" + three.toString() + ") / (" + testDiv.toString() + ") = " + three.divide(testDiv));
three.setReal(1);
three.setImag(4);
//testing equals method
System.out.println();
System.out.println("Testing equals method...");
if (three.equals(four) == true) {
System.out.println(three.toString() + " is equal to " + four.toString());
}
else {
System.out.println(three.toString() + " is not equal to " + four.toString());
}
Complex testEquals = new Complex(2, 3);
if (four.equals(testEquals) == true) {
System.out.println(four.toString() + " is equal to " + testEquals.toString());
}
else {
System.out.println(four.toString() + " is not equal to " + testEquals.toString());
}
}// end main method
}// end class
My first problem is that if I would call my add method on the three object [such as three.add(four) ] it completely changes the three object to the answer of three.add(four). What I did to get around that (and I'm assuming it's bad programming) is call the set methods to assign the three object back to what I needed.
My second problem is that the multiply and divide methods (that I have commented above with "****problem code****") are not reporting the correct complex number. The multiply problem code should show (-10.0 + 11.0i) in the tester but instead it shows (-10.0 + -22.0i) upon running. The divide problem code should show (1.0 + 1.0i) but instead it shows (1.0 + 0.7i) upon running.
To multiply a complex number by another complex number the formula is: (A + Bi) times (C + Di) = (AC - BD) + (AD + BC)i
To divide a complex number by another complex number the formula is: (A + Bi) divided by (C + Di) = (AC+BD)/(C2 + D2) + (BC-AD)/(C2 + D2)i
My key to convert from the letters of the formulas listed (A, B, C, D) and my own self-named variables is: A = real, B = imag, C = newReal, and D = newImag
This updates your instance variable real and you are using this updated real variable in the calculation of imaginary part of the complex variable which is obviously wrong.
Your code should be like this.