设计一个方法add,接收一个复数参数,返回两个复数加法的和(结果也是一个复数) (10分)
public double add(double a) { //x是实部,y是虚部
return (this.x+...后面怎么写
}
调用是类似于Complex com3=com1.add(com2)
设计一个方法add,接收一个复数参数,返回两个复数加法的和(结果也是一个复数) (10分)
public double add(double a) { //x是实部,y是虚部
return (this.x+...后面怎么写
}
调用是类似于Complex com3=com1.add(com2)
/**
* 定义并实现一个复数类(Complex),包含实部(real)及虚部(image)
* 两个属性,包含计算两个复数的和(add)、积(multiply)以及打印复数(print)
* 等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
* @remakeTODO
* @author Conquer丶ZF
* @date 2019年9月12日下午8:26:10
*/
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;
}
/**
* 重写toString方法,输出容易看的懂,方便
*/
@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){//虚部不为0时
real1 = (this.getReal() * com.getReal()) - (this.getImage() * com.getImage());//两个虚部相乘是负数
image1 = (this.getReal() * com.getImage()) + (this.getImage() * com.getReal());
}
else{//当有其中一个虚部为0时
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));
}
}