I have here a code that should print the sum and difference of two complex numbers. The instructions given are:
make the methods add
, subtract
, and print
to be void
and
test using the constructor's object.
public class Complex {
/**
* @param args
*/
public double real;
public double imag;
public String output = "";
public Complex(double real, double imag){
this.real += real;
this.imag += imag;
}
public Complex(){
real = 0;
imag = 0;
}
public double getReal(){
return real;
}
public void setReal(double real){
this.real = real;
}
public double getImag(){
return imag;
}
public void setImag(double imag){
this.imag = imag;
}
public void add(Complex num){
this.real = real + num.real;
this.imag = imag + num.imag;
}
public void subtract(Complex num){
this.real = real - num.real;
this.imag = imag - num.imag;
}
public void print(){
//
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(4.0, 8.5);
Complex c2 = new Complex(8.0, 4.5);
c1.add(c2);
c1.subtract(c2);
c1.print(); //expected answer 12.0 + 13.0i
//-4.0 - 4.0i
}
}
The expected answers are 12.0 + 13.0i and -4.0 - 4.0i. Please help me with the method print
. Thank you.