第三次实验报告
20145217 《Java程序设计》第三次实验报告
内容总结
首先由20145222黄亚其同学进行complex类编写,编写完成后发至自己的shiyansan项目,同时我也是shiyansan项目的管理员有权限进行修改。
后由我进行complex类测试代码编写,我从实验三项目上下载complex类。
在完成测试以后,我将代码回传到分支shiyansan1。
黄亚奇再从shiyan1分支上下载程序代码。
代码如下:
产品代码
package complex;
public class Complex
{
double real, unr; //实部和虚部
public Complex() //默认构造方法
{
this.real = 0;
this.unr = 0;
}
public Complex(double real, double unr)
{
this.real = real;
this.unr = unr;
}
public double getReal() {
return this.real;
}
public double getImage() {
return this.unr;
}
public double getReal(Complex c) {
return c.real;
}
public double getImage(Complex c) {
return c.unr;
} //得到复数c的虚部
public void setReal(double real) {
this.real = real;
} //设置实部
public void setImage(double unr) {
this.unr = unr;
} //设置虚部
public Complex addComplex(Complex a, Complex b) //两个复数相加,结果返回
{
Complex temp = new Complex();
temp.real = a.real + b.real;
temp.unr = a.unr + b.unr;
return temp;
}
public Complex decComplex(Complex a, Complex b) //两个复数相减,结果返回
{
Complex temp = new Complex();
temp.real = a.real - b.real;
temp.unr = a.unr - b.unr;
return temp;
}
public Complex mulComplex(Complex a, Complex b) //两个复数相乘,结果返回
{
Complex temp = new Complex();
temp.real = a.real * b.real - a.unr * b.unr;
temp.unr = a.real * b.unr + a.unr * b.real;
return temp;
}
public Complex divComplex(Complex a, Complex b) //两个复数相除,结果返回
{
Complex temp = new Complex();
temp.real = (a.real * b.real + a.unr * b.unr) / (b.real * b.real + b.unr * b.unr);
temp.unr = (a.unr * b.real - a.real * b.unr) / (b.real * b.real + b.unr * b.unr);
return temp;
}
public void printComplex()
{
System.out.println("" + this.real + "+" + this.unr + "i");
}
}
测试代码
package complex;
public class Comlextest {
public static void main(String[] args) //测试代码
{
Complex cc=new Complex(1,2);
cc.printComplex();
Complex dd=new Complex(4,3);
dd.printComplex();
System.out.println();
Complex ff=new Complex();
ff=ff.addComplex(cc,dd);
ff.printComplex();
ff=ff.decComplex(cc,dd);
ff.printComplex();
ff=ff.mulComplex(cc,dd);
ff.printComplex();
ff=ff.divComplex(cc,dd);
ff.printComplex();
System.out.println();
cc.printComplex();
cc=new Complex(2,5);
cc.printComplex();
cc=new Complex(5,8);
cc.printComplex();
cc=new Complex(6,8);
cc.printComplex();
System.out.println();
cc.setReal(123);
cc.setImage(456);
cc.printComplex();
System.out.println(""+cc.getReal()+"+"+cc.getImage()+"i");
}
}