实验二报告

20145339顿珠达杰 实验二"Java面向对象程序设计"

程序设计过程

实验内容

使用TDD的方式设计实现复数类 Complex 自己请教张薇用这个东西 所以直接用了梓靖的代码 重新完成她的实验。

编写代码:

import org.junit.Test; public class ComplexNumberTest { @Test public void TestFunction1() { ComplexNumber complexnumber = new ComplexNumber(3, 5); ; assert complexnumber.mdRealPart == 3 : "ComplexNumber(double r,double I)构造函数有错误"; assert complexnumber.mdImaginPart == 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方法有错误";

}

进行单元测试:

建模:

 

实验体会:

自己还是没学到很多.得加油了。

posted on 2016-04-17 21:30  20145339顿珠  阅读(163)  评论(2编辑  收藏  举报