20145330《Java程序设计》第二次实验报告

实验二 Java面向对象程序设计##

实验内容##

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

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

3.初步掌握UML建模

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

5.了解设计模式

实验要求

1.没有Linux基础的同学建议先学习《Linux基础入门(新版)》《Vim编辑器》 课程;

2.完成实验、撰写实验报告,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等)、解决办法;

TDD##

实验步骤

  • 明确当前要完成的功能,记录成一个测试列表
  • 快速完成编写针对此功能的测试用例
  • 测试代码编译不通过
  • 编写产品代码
  • 测试通过
  • 对代码进行重构,并保证测试通过
  • 循环完成所有功能的开发

1.创建test

2.输入源代码


package com.example.junit; /**
* Created by lenovo on 2016/4/12.
*/
public class MyUtil { public static String percentage2fivegrade(int grade)
{
if((grade<0))
return "错误";
else if (grade<60)
return "不及格";
else if (grade<70)
return "及格";
else if (grade<80)
return "中等";
else if (grade<90)
return "良好";
else if (grade<=1000)
return "优秀";
else
return "错误"; } }

3.输入测试代码


package com.example.junit; import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals; /**
* Created by lenovo on 2016/4/12.
*/
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));
} @Before
public void setUp() throws Exception { }
}

4.进行测试直到测试结果为绿条成功

  • 问题与解决方法

    • 问题:输入完毕后下划线有红色部分存在语法错误
    • 方法:MyUtil类还不存在,类中方法也不存在,我们在目录新建一个MyUtil类,即可实现相关方法

练习内容

  • 写一个类测试编写的复数类的方法

    • 构造函数,将实部,虚部都置为0
    • 构造函数,创建复数对象的同时完成复数的实部,虚部的初始化
    • 设置实部,设置虚部
    • 复数相加
    • 复数相减
    • 复数相乘
  • 源代码

public class Complex { private double realPart;
private double imaginPart;
public Complex(){
double realPart;
double imaginPart;
}
public Complex(double r,double i){
double realPart;
double imaginPart;
this.realPart=r;
this.imaginPart=i;
}
public double getRealPart(){
return realPart;
}
public double getImaginPart(){
return imaginPart;
}
public void setRealPart(double d){
this.realPart=d;
} public void setImaginPart(double d) {
this.imaginPart =d;
}
public void ComplexAdd(Complex c){
this.realPart+=c.realPart;
this.imaginPart+=c.imaginPart;
}
public void ComplexAdd(double c){
this.realPart+=c;
}
public void ComplexMinus(Complex c){
this.realPart-=c.realPart;
this.imaginPart-=c.imaginPart;
}
public void ComplexMinus(double c){
this.realPart-=c;
}
public void ComplexMulti(Complex c){
this.realPart*=c.realPart;
this.imaginPart*=c.imaginPart;
}
public void ComplexMulti(double c){
this.realPart*=c;
} }

  • 测试代码

import junit.framework.TestCase;
import org.junit.Test; import static org.junit.Assert.*; /**
* Created by lenovo on 2016/4/13.
*/
public class ComplexTest extends TestCase{
Complex c1=new Complex(3,5);
Complex c2=new Complex(3,5);
double a=5; @Test
public void testComplexAdd() throws Exception {
c1.ComplexAdd(c2);
assertEquals(6.0,c1.getRealPart());
assertEquals(10.0,c1.getImaginPart());
} @Test
public void testComplexMinus() throws Exception {
c1.ComplexMinus(c2);
assertEquals(0.0,c1.getRealPart());
assertEquals(0.0,c1.getImaginPart()); } @Test
public void testComplexMulti() throws Exception {
c1.ComplexMulti(c2);
assertEquals(9.0,c1.getRealPart());
assertEquals(25.0,c1.getImaginPart()); }

  • 运行结果

UML##

在Umbrello中UML图是可以转化成Java代码的,有Java代码也可以生成UML图。

心得体会

学会了单元测试觉得方便了很多。它是一种验证行为,程序中的每一项功能都是测试来验证它的正确性。它为以后的开发提供支援。而且它对代码的重构提供了保障,这样我们就可以更自由的对程序进行改进。

单元测试还是一种设计行为,编写单元测试使我们观察思考,使我们更好地把程序设计成易于调试和可测试的

通过这次试验,我有更加深入的学习了Java,并逐渐可以将它合理的运用。

工具##

1.JUnit

2.umbrello

3.StarUML

最新文章

  1. Fork函数初识
  2. string类find函数返回值判定
  3. C#函数式编程之部分应用
  4. Mount NAS Storage in Linux Overview 转载
  5. HDU 4054
  6. numpy+scipy+matlotlib+scikit-learn的安装及问题解决
  7. ecstore后台规格超过一定数量保存丢失
  8. 阿里云ECS每天一件事D1:配置SSH
  9. 0.关于TCP协议的一些总结
  10. BZOJ第1页养成计划
  11. C语言第一次博客作业 陈张鑫
  12. MyCP(课下作业)
  13. Linux c使用gumbo库解析页面表单信息(一)
  14. MapReduce框架原理-MapTask工作机制
  15. Coursera Deep Learning 2 Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization - week2, Optimization algorithms
  16. Linux下安装Julia1.0.0
  17. ipad The data couldn’t be read because it isn’t in the correct format
  18. 20155309南皓芯 实验2 Windows口令破解
  19. Entity Framework Core 入门(2)
  20. C++ 11 nullptr关键字

热门文章

  1. 详细的JavaScript事件使用指南
  2. Solr入门之(4)配置文件solr.xml
  3. .NET Nancy 详解(一) 初识
  4. WPF 自定义Metro Style窗体
  5. hdu 5294 最短路+最大流 ***
  6. hdu 4044 2011北京赛区网络赛E 树形dp ****
  7. 官方Tomcat 8.0.24 Web漏洞整改记录
  8. Lucene.net 全文检索 盘古分词
  9. html table之 全选,全不选
  10. input上下居中问题