20145302張薇《Java程式設計》實驗二報告

20145302張薇發表於2016-04-17

20145302張薇《Java程式設計》實驗二:Java物件導向程式設計

使用TDD的方式設計實現複數類:Complex

測試程式碼

import org.junit.Test;

public class ComplexNumberTest {
    @Test
    public void TestFunction1() {
        ComplexNumber complexnumber = new ComplexNumber(1, 3);
        ;
        assert complexnumber.m_dRealPart == 1 : "ComplexNumber(double r,double I)建構函式有錯誤";
        assert complexnumber.m_dImaginPart == 3 : "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(2);
        complexnumber.SetImaginaryPart(5);
        assert complexnumber.GetRealPart() == 2 : "GetRealPart方法有錯誤";
        assert complexnumber.GetImaginaryPart() == 5 : "GetImaginaryPart方法有錯誤";
    }

    @Test
    public void TestStringWay() {
        ComplexNumber complexnumber = new ComplexNumber(2,6);
        assert complexnumber.toString()=="2.0+6.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.ComplexMinusl(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.ComplexMultil(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方法有錯誤";

    }

}

實驗程式碼

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 ComplexMinusl(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 ComplexMultil(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";
    }
}

實驗結果截圖

20145302張薇《Java程式設計》實驗二報告

  • 說是String方法有問題,其他的成功了。

實驗調整截圖

20145302張薇《Java程式設計》實驗二報告

  • 我試了,在別人的電腦上可以執行成功,但是這裡出錯了,說是用到了java.lang.String方法。

建模截圖

20145302張薇《Java程式設計》實驗二報告

實驗體會

  • 實驗最重要的是動手做,自己獨立上手一遍會記得更牢固。

相關文章