java设计,请各位高手帮帮忙!

一个复数Complex由两部分组成:实部realPart和虚部imaginaryPart,两个复数可进行加、减、乘、除四则运算。试设计一个带有四则运算的复数类,并编写主程... 一个复数Complex由两部分组成:实部realPart和虚部imaginaryPart,两个复数可进行加、减、乘、除四则运算。试设计一个带有四则运算的复数类,并编写主程序演示该类的用法。 展开
 我来答
flyingFish211
2010-11-25 · TA获得超过2.1万个赞
知道大有可为答主
回答量:1.5万
采纳率:50%
帮助的人:1.2亿
展开全部
public class Fushu {

private int realPart;

private int imaginaryPart;

public Fushu(int real, int imaginary) {
realPart = real;
imaginaryPart = imaginary;
}

public static void main(String[] args) {

Fushu fushu1 = new Fushu(6, 3);
Fushu fushu2 = new Fushu(1, 1);

System.out.println("+++++ " + add(fushu1, fushu2));
System.out.println("----- " + reduce(fushu1, fushu2));
System.out.println("***** " + multi(fushu1, fushu2));
System.out.println("//// " + div(fushu1, fushu2));

}

/// +++++++++++
public static Fushu add(Fushu fushu1, Fushu fushu2){

int real = fushu1.getRealPart() + fushu2.getRealPart();
int imag = fushu1.getImaginaryPart() + fushu2.getImaginaryPart();

return new Fushu(real, imag);
}

// --------------
public static Fushu reduce(Fushu fushu1, Fushu fushu2){
int real = fushu1.getRealPart() - fushu2.getRealPart();
int imag = fushu1.getImaginaryPart() - fushu2.getImaginaryPart();

return new Fushu(real, imag);
}

// **********
public static Fushu multi(Fushu fushu1, Fushu fushu2){

int real = (fushu1.getRealPart() * fushu2.getRealPart())
- (fushu1.getImaginaryPart() * fushu2.getImaginaryPart());
int imag = fushu1.getRealPart() * fushu2.getImaginaryPart()
+ fushu2.getRealPart() * fushu1.getImaginaryPart();

return new Fushu(real, imag);
}

// ///////////////
public static Fushu div(Fushu fushu1, Fushu fushu2){
int real1 = fushu1.getRealPart();
int real2 = fushu2.getRealPart();
int image1 = fushu1.getImaginaryPart();
int image2 = fushu2.getImaginaryPart();

int real = (real1 * real2 + image1 * image2) / (real2* real2 + image2*image2);
int image = (real2 * image1 - real1 * image2) / (real2* real2 + image2*image2);

return new Fushu(real, image);
}

public int getImaginaryPart() {
return imaginaryPart;
}

public void setImaginaryPart(int imaginaryPart) {
this.imaginaryPart = imaginaryPart;
}

public int getRealPart() {
return realPart;
}

public void setRealPart(int realPart) {
this.realPart = realPart;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式