2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

实验内容

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

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

3.初步掌握UML建模

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

5.了解设计模式

(一)单元测试

对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如Java里单元指一个类。编程是智力活动,不是打字,编程前要把干什么、如何干想清楚才能把程序写对、写好。与目前不少同学一说编程就打开编辑器写代码不同,我希望同学们养成一个习惯,当你们想用程序解决问题时,要会写三种码:

  • 伪代码
  • 产品代码
  • 测试代码

    在 IDEA中我们把产品代码放在src目录中,把测试代码放在test目录中,右键单击项目,在弹出的菜单中选择New->Directory新建一个目录:

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

    在IDEA中运行结果如下,测试结果符合预期:

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

    在Eclipse中运行结果如下,测试结果符合预期:

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

    我们不能只测试正常情况,下面看看异常情况如何,比如输入为负分或大于100的成绩

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

    测试结果如下:

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

(二)TDD(Test Driven Devlopment, 测试驱动开发)

  • 安装JUnit

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告
  • 测试代码
import org.junit.Test;
import junit.framework.TestCase;
public class MyUtilTest extends TestCase {
@Test
public void testNormal() {
assertEquals("不及格", MyUtil.percentage2fivegrade(55));
assertEquals("及格", MyUtil.percentage2fivegrade(65));
assertEquals("中等", MyUtil.percentage2fivegrade(75));
assertEquals("良好", MyUtil.percentage2fivegrade(85));
assertEquals("优秀", MyUtil.percentage2fivegrade(95));
}
}

运行结果如下

2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

我们增加一个测试异常情况的用例testException和一个测试边界情况的用例testBoundary

2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

(三)设计模式初步

2018-2019-2 20175329许钰玮 实验二《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";
}
}
  • 实验结果截图

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

  • string语法有误调整如下

    2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

(五)UML图设计

2018-2019-2 20175329许钰玮 实验二《Java面向对象程序设计》实验报告

上一篇:libuv 初窥--转


下一篇:Java编程思想学习(五)----第5章:初始化与清理