20145321 实验二实验报告

20145321 实验二实验报告

实验内容

1.初步掌握单元测试和TDD

2.理解并掌握面向对象三要素:封装、继承、多态

3.初步掌握UML建模

4.熟悉S.O.L.I.D原则

5.了解设计模式

PSP统计

 

步骤耗时百分比
需求分析 30min 15%
设计 60min 30%
代码实现 60min 30%
测试 30min 15%
分析总结 20min 10%

实验步骤

1.使用TDD的方式设计关实现复数类Complex

(1)伪代码:

 复数类Complex
 复数=实部+虚部i
 复数相加=(实部+实部)+(虚部+虚部)i
 复数相减=(实部-实部)+(虚部-虚部)i
 打印复数
 虚部>0,“实部”+“+”+“虚部”i
 虚部<0,“实部”+“-”+“虚部”i
 虚部=0,“实部”

(2)产品代码:

 class ComplexNumber {
 private double m_dRealPart,m_dImaginPart;

 public ComplexNumber(){
    m_dRealPart = 0;
    m_dImaginPart = 0;
 }
 public ComplexNumber(double r,double I){
    m_dRealPart = r;
    m_dImaginPart = i;
 }
 public  double GetRealPart(){
    return m_dRealPart;
 }
 public double GetImaginaryPart(){
    return m_dImaginPart;
 }
 public void SetRealPart(double d){
    m_dRealPart = d;
 }
 public void SetImaginaryPart(double d){
    m_dImaginPart = d;
 }
 public ComplexNumber ComplexAdd(ComplexNumber c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart+c.GetRealPart());
    result.SetImaginaryPart(this.m_dImaginPart+c.GetImaginaryPart());
    return result;
 }
 public ComplexNumber ComplexAdd(double c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart+c);
    result.SetImaginaryPart(this.m_dImaginPart);
    return result;
 }
 public ComplexNumber ComplexMinus(ComplexNumber c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart-c.GetRealPart());
    result.SetImaginaryPart(this.m_dImaginPart-c.GetImaginaryPart());
    return result;
 }
 public ComplexNumber ComplexMinus(double c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart-c);
    result.SetImaginaryPart(this.m_dImaginPart);
    return result;
 }
 public ComplexNumber ComplexMuti(ComplexNumber c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart*c.GetRealPart()-this.m_dImaginPart*c.GetImaginaryPart());
    result.SetImaginaryPart(this.m_dRealPart*c.GetImaginaryPart()+this.m_dImaginPart*c.GetRealPart());
    return result;
 }
 public ComplexNumber ComplexMuti(double c){
    ComplexNumber result = new ComplexNumber();
    result.SetRealPart(this.m_dRealPart*c);
    result.SetImaginaryPart(this.m_dImaginPart*c);
    return result;
 }
 public String toString(){
    return ""+this.m_dRealPart+"+"+this.m_dImaginPart+"i";
 }
 }

(3)测试代码:

 import org.junit.Test;  
 @Test
 public void TestStringWay() {
    ComplexNumber complexnumber = new ComplexNumber(7,3);
    System.out.println(complexnumber.toString());
    assertFquals(73.0+3.0i",complexnumber.toString());
 }

2.单元测试

3.StarUML建模

posted on 2016-04-17 21:27  20145321曾子誉  阅读(154)  评论(2编辑  收藏  举报

导航