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

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

posted @ 2016-04-14 18:37  20145330孙文馨  阅读(168)  评论(1编辑  收藏  举报