博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20145302张薇《Java程序设计》实验二报告
阅读量:5213 次
发布时间:2019-06-14

本文共 5607 字,大约阅读时间需要 18 分钟。

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";    }}

实验结果截图

884662-20160417213201707-2083031246.png

  • 说是String方法有问题,其他的成功了。

实验调整截图

884662-20160417213352723-512834619.png

  • 我试了,在别人的电脑上可以运行成功,但是这里出错了,说是用到了java.lang.String方法。

建模截图

884662-20160417213537645-856948106.jpg

实验体会

  • 实验最重要的是动手做,自己独立上手一遍会记得更牢固。

转载于:https://www.cnblogs.com/5302v/p/5402330.html

你可能感兴趣的文章
NOIP练习赛题目4
查看>>
工作996生病ICU,程序员的工作就不能没有加班?
查看>>
Android Eclipse JNI 调用 .so文件加载【转】
查看>>
iOS基于B站的IJKPlayer框架的流媒体探究
查看>>
ios之库Protobuf的使用
查看>>
Hibernate初探之单表映射——第二章:Hibernate进阶
查看>>
QuickHit游戏
查看>>
C#之四十 Winform MD5加密
查看>>
ASP.NET(C#)——GridView刷新
查看>>
ansible案例-安装nginx
查看>>
测试与优化
查看>>
IE9以下浏览器提示升级
查看>>
CSS 3 选择器
查看>>
又是1年过去了。。。
查看>>
我的网站PR升了没有?
查看>>
普通软件项目开发过程规范(一)—— 项目阶段 (转)
查看>>
mongodb 基础知识
查看>>
AngularJs学习笔记(一)----------关于数据绑定
查看>>
关于Junit、Hamcrest、Eclemma的安装配置,以及简易程序Junit测试、覆盖测试
查看>>
socket编程实战-connect超时问题
查看>>