设计一个复数类--求帮忙
因为我刚接触Java不久。。思绪有点乱,写不出~~ 先在此谢谢大家~~题目为:
设计一个复数类,要求:
(1)在复数内部用双精度浮点数定义其“实部”和“虚部”。
(2)实现3个构造函数:第1个构造函数没有参数;第2个构造函数将双精度浮点数赋给复数的“实部”,“虚部”为0;
第3个构造函数将两个双精度浮点数分别赋给复数的“实部”和“虚部”;
(3)编写获取和修改复数的“实部”和“虚部”的成员函数;
(4)编写实现复数的剪发、乘法运算的成员函数;
(5)设计一个测试主函数,使其实际运行验证类中各成员函数的正确性。
因为我刚接触Java不久。。思绪有点乱,写不出~~ 先在此谢谢大家~~题目为:
设计一个复数类,要求:
(1)在复数内部用双精度浮点数定义其“实部”和“虚部”。
(2)实现3个构造函数:第1个构造函数没有参数;第2个构造函数将双精度浮点数赋给复数的“实部”,“虚部”为0;
第3个构造函数将两个双精度浮点数分别赋给复数的“实部”和“虚部”;
(3)编写获取和修改复数的“实部”和“虚部”的成员函数;
(4)编写实现复数的剪发、乘法运算的成员函数;
(5)设计一个测试主函数,使其实际运行验证类中各成员函数的正确性。
private double real = 0;//实部
private double imaginary = 0;//虚部
/*
* 构造方法
* */
public Complex(){
};
public Complex(int real){
this.setReal(real);
}
public Complex(int real,int imaginary){
this.setReal(real);
this.setImaginary(imaginary);
}
/*
* getter and setter
**/
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
} public double getImaginary() {
return imaginary;
} public void setReal(double real) {
this.real = real;
} public double getReal() {
return real;
}
public String toString(){
String c;
if(real==0){
c = "" ;
if(imaginary==0){
c = c + "0";
}else{
c = c + "+" + imaginary + "";
}
}else{
c = real + "";
if(imaginary==0){
c = c + "";
}else{
c = c + "+" + imaginary + "";
}
}
return c;
}
//N个数的加法
public Complex sum(Complex c1,Complex ... args){
Complex sum = c1;
for (int i=0;i<args.length;i++) {
sum.real = sum.real + args[i].real;
sum.imaginary = sum.imaginary + args[i].imaginary;
}
return sum;
}
//减法,C1被减,其他都是减数
public Complex mus(Complex c1,Complex ... args){
Complex mus = c1;
for (int i=0;i<args.length;i++) {
mus.real = mus.real - args[i].real;
mus.imaginary = mus.imaginary - args[i].imaginary;
}
return mus;
}
//乘法
public Complex cheng(Complex c1,Complex ... args){
Complex result=c1;
for (int i=0;i<args.length;i++) {
result.real = c1.real*result.real + c1.imaginary*result.imaginary;
result.imaginary = c1.imaginary*result.real + c1.real*result.imaginary;
}
return result;
}
}