20145329 《Java程序设计》实验二总结
实验指导教师:娄嘉鹏老师
实验日期:2016.4.12
实验时间:15:30~17:30
实验序号:实验二
实验名称:Java面向对象程序设计
实验目的与要求:
1.初步掌握单元测试和TDD
2.理解并掌握面向对象三要素:封装、继承、多态
3.初步掌握UML建模
4.熟悉S.O.L.I.D原则
5.了解设计模式
实验内容
1.使用TDD的方式实现设计复杂类Complex
2.实验报告中统计自己的PSP
3.实现要有伪代码、产品代码、测试代码
4.总结单元测试的好处
实验过程中
伪代码
定义实部和虚部
默认构造方法
带参数的构造方法
得到实部,得到虚部
得到复数c的实部,得到复数c的虚部
设置实部,设置虚部
两个复数相加,结果返回
两个复数相减,结果返回
两个复数相乘,结果返回
测试代码
import org.junit.Test;
import static org.junit.Assert.*;
public class ComplexNumberTest {
@Test
public void testGetRealPart() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
assert cc.m_dRealPart==3:
"testGetRealPart()处理有错误";
}
@Test
public void testGetImaginaryPart() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
assert cc.m_dImaginaryPart==5:
"testGetRealPart()处理有错误";
}
@Test
public void testSetRealPart() throws Exception {
ComplexNumber cc=new ComplexNumber();
assert cc.m_dRealPart==0:
"testSetRealPart()处理有错误";
}
@Test
public void testSetImaginaryPart() throws Exception {
ComplexNumber cc=new ComplexNumber();
assert cc.m_dImaginaryPart==0:
"testSetImaginaryPart()处理错误";
}
@Test
public void testComplexAdd() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexAdd(cc,dd);
assert ff.m_dRealPart==6&&ff.m_dImaginaryPart==8:
"ComplexAdd()方法处理错误";
}
@Test
public void testComplexMinus() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexMinus(cc,dd);
assert ff.m_dRealPart==0&&ff.m_dImaginaryPart==2:
"ComplexMinus()方法处理错误";
}
@Test
public void testComplexMulti() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber dd=new ComplexNumber(3,3);
ComplexNumber ff=cc.ComplexMulti(cc,dd);
assert ff.m_dRealPart==-6&&ff.m_dImaginaryPart==24:
"ComplexMulti()方法处理错误";
}
@Test
public void testComplexAdd1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexAdd1(2);
assert ff.m_dRealPart==5&&ff.m_dImaginaryPart==5:
"ComplexAdd1()方法处理错误";
}
@Test
public void testComplexMinus1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexMinus1(2);
assert ff.m_dRealPart==1&&ff.m_dImaginaryPart==5:
"ComplexMinus1()方法处理错误";
}
@Test
public void testComplexMulti1() throws Exception {
ComplexNumber cc=new ComplexNumber(3,5);
ComplexNumber ff=cc.ComplexMulti1(2);
assert ff.m_dRealPart==6&&ff.m_dImaginaryPart==10:
"ComplexMulti1()方法处理错误";
}
}
产品代码
public class ComplexNumber {
double m_dRealPart, m_dImaginaryPart;
public ComplexNumber() {
this.m_dRealPart = 0;
this.m_dImaginaryPart = 0;
}
public ComplexNumber(double real, double img) { this.m_dRealPart = real;
this.m_dImaginaryPart = img;
}
public double GetRealPart() {
return this.m_dRealPart;
}
public double GetImaginaryPart() {
return this.m_dImaginaryPart;
}
public double GetRealPart(ComplexNumber c) {
return c.m_dRealPart;
}
public double GetImaginaryPart(ComplexNumber c) {
return c.m_dImaginaryPart;
}
public void SetRealPart(double real) {
this.m_dRealPart = real;
}
public void SetImagijnPart(double img) {
this.m_dImaginaryPart = img;
}
public ComplexNumber ComplexAdd(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber(); temp.m_dRealPart = a.m_dRealPart + b.m_dRealPart;
temp.m_dImaginaryPart = a.m_dImaginaryPart + b.m_dImaginaryPart;
return temp;
}
public ComplexNumber ComplexMinus(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber();
temp.m_dRealPart = a.m_dRealPart - b.m_dRealPart;
temp.m_dImaginaryPart = a.m_dImaginaryPart - b.m_dImaginaryPart;
return temp;
}
public ComplexNumber ComplexMulti(ComplexNumber a, ComplexNumber b) {
ComplexNumber temp = new ComplexNumber();
temp.m_dRealPart = a.m_dRealPart * b.m_dRealPart - a.m_dImaginaryPart * b.m_dImaginaryPart;
temp.m_dImaginaryPart = a.m_dRealPart * b.m_dImaginaryPart + a.m_dImaginaryPart * b.m_dRealPart;
return temp;
}
public ComplexNumber ComplexAdd1(double a) {
this.m_dRealPart = this.m_dRealPart + a;
return this;
}
public ComplexNumber ComplexMinus1(double a) {
this.m_dRealPart = this.m_dRealPart - a;
return this;
}
public ComplexNumber ComplexMulti1(double a) {
this.m_dRealPart = this.m_dRealPart * a;
this.m_dImaginaryPart = this.m_dImaginaryPart * a;
return this;
}
public String toString(){
return this.m_dRealPart+"+"+this.m_dImaginaryPart+"i";
}
}
测试结果
UML建模
PSP时间
步骤 | 耗时(min | 百分比 |
---|---|---|
需求分析 | 40 | 20% |
设计 | 30 | 15% |
代码实现 | 50 | 25% |
测试 | 50 | 25% |
分析总结 | 30 | 15% |