20145325张梓靖 实验二"Java面向对象程序设计"
20145325张梓靖 实验二"Java面向对象程序设计"
程序设计过程
实验内容
使用TDD的方式设计实现复数类 Complex
编写代码
- 设计实现复数类 Complex,复数类 Complex的方法设计有:定义构造函数、更改复数内容、返回复数内容,以及复数间相加、减、乘运算。
- 设计的Test代码第一代:
import org.junit.Test;
public class ComplexNumberTest {
@Test
public void TestFunction1() {
ComplexNumber complexnumber = new ComplexNumber(3, 5);
;
assert complexnumber.m_dRealPart == 3 : "ComplexNumber(double r,double I)构造函数有错误";
assert complexnumber.m_dImaginPart == 5 : "ComplexNumber(double r,double I)构造函数有错误";
}
@Test
public void TestFunction2() {
ComplexNumber complexnumber = new ComplexNumber();
assert complexnumber.m_dImaginPart == 0 : "ComplexNumber()构造函数有错误";
assert complexnumber.m_dRealPart == 0 : "ComplexNumber()构造函数有错误";
}
@Test
public void TestSetWay() {
ComplexNumber complexnumber = new ComplexNumber();
complexnumber.SetRealPart(1);
complexnumber.SetImaginaryPart(1);
assert complexnumber.m_dRealPart == 1 : "SetRealPart方法有错误";
assert complexnumber.m_dImaginPart == 1 : "SetImaginaryPart方法有错误";
}
@Test
public void TestgetWay() {
ComplexNumber complexnumber = new ComplexNumber();
complexnumber.SetRealPart(3);
complexnumber.SetImaginaryPart(5);
assert complexnumber.GetRealPart() == 3 : "GetRealPart方法有错误";
assert complexnumber.GetImaginaryPart() == 5 : "GetImaginaryPart方法有错误";
}
@Test
public void TestStringWay() {
ComplexNumber complexnumber = new ComplexNumber(3,5);
assert complexnumber.toString()=="3.0+5.0i":"toString方法有错误";
}
@Test
public void TestAddWay1() {
ComplexNumber complexnumber1 = new ComplexNumber(1,1);
ComplexNumber complexnumber2 = new ComplexNumber(2,2);
ComplexNumber complexnumber3=complexnumber1.ComplexAdd1(complexnumber2);
assert complexnumber3.m_dRealPart == 3 : "ComplexAdd方法有错误";
assert complexnumber3.m_dImaginPart == 3 : "ComplexAdd方法有错误";
}
@Test
public void TestAddWay2() {
ComplexNumber complexnumber1 = new ComplexNumber(1,1);
ComplexNumber complexnumber2=complexnumber1.ComplexAdd2(2);
assert complexnumber2.m_dRealPart == 3 : "ComplexAdd方法有错误";
assert complexnumber2.m_dImaginPart == 1 : "ComplexAdd方法有错误";
}
@Test
public void TestMinusWay1() {
ComplexNumber complexnumber1 = new ComplexNumber(1,1);
ComplexNumber complexnumber2 = new ComplexNumber(2,2);
ComplexNumber complexnumber3=complexnumber2.ComplexMinus1(complexnumber1);
assert complexnumber3.m_dRealPart == 1 : "ComplexMinus方法有错误";
assert complexnumber3.m_dImaginPart == 1 : "ComplexMinus方法有错误";
}
@Test
public void TestMinusWay2() {
ComplexNumber complexnumber1 = new ComplexNumber(5,1);
ComplexNumber complexnumber2=complexnumber1.ComplexMinus2(1);
assert complexnumber2.m_dRealPart == 4 : "ComplexMinus方法有错误";
assert complexnumber2.m_dImaginPart == 1 : "ComplexMinus方法有错误";
}
@Test
public void TestMultiWay1() {
ComplexNumber complexnumber1 = new ComplexNumber(3,3);
ComplexNumber complexnumber2 = new ComplexNumber(3,5);
ComplexNumber complexnumber3=complexnumber1.ComplexMulti1(complexnumber2);
assert complexnumber3.m_dRealPart == -6 : "ComplexMinus方法有错误";
assert complexnumber3.m_dImaginPart == 24 : "ComplexMinus方法有错误";
}
@Test
public void TestMultiWay2() {
ComplexNumber complexnumber1 = new ComplexNumber(3,3);
ComplexNumber complexnumber2=complexnumber1.ComplexMulti2(2);
assert complexnumber2.m_dRealPart == 6 : "ComplexMinus方法有错误";
assert complexnumber2.m_dImaginPart == 6 : "ComplexMinus方法有错误";
}
}
- 根据复数类 Complex的设计构思,编写出复数类 Complex的代码
public class ComplexNumber {
public double m_dRealPart;
public double m_dImaginPart;
public ComplexNumber(){
this.m_dImaginPart=0;
this.m_dRealPart=0;
}
public ComplexNumber(double r,double I){
this.m_dRealPart=r;
this.m_dImaginPart=I;
}
public double GetRealPart(){
return this.m_dRealPart;
}
public double GetImaginaryPart(){
return this.m_dImaginPart;
}
public void SetRealPart(double d){
this.m_dRealPart=d;
}
public void SetImaginaryPart(double d){
this.m_dImaginPart=d;
}
public ComplexNumber ComplexAdd1(ComplexNumber c){
this.m_dRealPart=this.m_dRealPart+c.m_dRealPart;
this.m_dImaginPart=this.m_dImaginPart+c.m_dImaginPart;
return this;
}
public ComplexNumber ComplexAdd2(double c){
this.m_dRealPart=this.m_dRealPart+c;
return this;
}
public ComplexNumber ComplexMinus1(ComplexNumber c) {
this.m_dRealPart = this.m_dRealPart - c.m_dRealPart;
this.m_dImaginPart = this.m_dImaginPart - c.m_dImaginPart;
return this;
}
public ComplexNumber ComplexMinus2(double c){
this.m_dRealPart=this.m_dRealPart-c;
return this;
}
public ComplexNumber ComplexMulti1(ComplexNumber c){
double a=this.m_dImaginPart;
double b=this.m_dRealPart;
this.m_dRealPart=b*c.m_dRealPart-(a*c.m_dImaginPart);
this.m_dImaginPart=b*c.m_dImaginPart+a*c.m_dRealPart;
return this;
}
public ComplexNumber ComplexMulti2(double c){
double a=this.m_dImaginPart;
double b=this.m_dRealPart;
this.m_dRealPart=this.m_dRealPart*c;
this.m_dImaginPart=this.m_dImaginPart*c;
return this;
}
public String toString(){
return this.m_dRealPart+"+"+this.m_dImaginPart+"i";
}
}
- 对复数类 Complex的代码进行单元测试
发现错误,是编写的Complex的代码出现了错误?在TestStringWay
中增加输出语句,输出toString
的结果:
-
没错误啊,答案和预料的一样。那么问题就是Test代码有问题了
-
所以修改为用
assertFquals
@Test
public void TestStringWay() {
ComplexNumber complexnumber = new ComplexNumber(3,5);
System.out.println(complexnumber.toString());
assertFquals("3.0+5.0i",complexnumber.toString());
}
- 建模结果截图: