package monster.zf.Test;
public class Complex {
private double real;
private double image;
public Complex() {
super();
}
public Complex(double real, double image) {
super();
this.real = real;
this.image = image;
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImage() {
return image;
}
public void setImage(double image) {
this.image = image;
}
@Override
public String toString() {
return "(real=" + real + ", image=" + image +"i"+")";
}
public void add(Complex com){
double real1 = this.getReal() + com.getReal();
double image1 = this.getImage() + com.getImage();
printComplex(real1,image1);
}
public void multiply(Complex com){
double real1;
double image1;
if(this.getImage() != 0 && com.getImage() != 0){
real1 = (this.getReal() * com.getReal()) - (this.getImage() * com.getImage());
image1 = (this.getReal() * com.getImage()) + (this.getImage() * com.getReal());
}
else{
real1 = (this.getReal() * com.getReal());
image1 = (this.getReal() * com.getImage()) + (this.getImage() * com.getReal());
}
printComplex(real1,image1);
}
public void printComplex(double real, double image){
System.out.println(new Complex(real,image));
}
}
package monster.zf.Test;
public class ComplexTest {
public static void main(String[] args) {
Complex com = new Complex(3, 4);
System.out.print(com.getReal() + "+" + com.getImage()+"i" + "=");
com.add(com);
System.out.print(com.getReal() + "*" + com.getImage() +"i"+ "=");
com.multiply(com);
}
}
