zoukankan      html  css  js  c++  java
  • 20145110 《Java程序设计》第二次实验报告

    • 实验内容与体会:

    TDD

    实验步骤

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

    创建test后输入源代码

    package com.example.junit;
    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 "错误";
    
             }
    
        }
    

    输入测试代码

    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 {
    
            }
        }
    

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

    练习内容

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

    • 构造函数,将实部,虚部都置为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.*;
     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());
    
        }
    

    实验感想总结

    在本次试验中,我们学习了Java代码的单元测试,知道了怎样去编写出更完整的代码,在不断地单元测试改进中,代码的稳定性越来越高。通过这次试验,我有更加深入的学习了Java,并逐渐可以将它合理的运用。

  • 相关阅读:
    Golang 用go-sql-driver 调用MySQL存储过程时的问题排查
    mysqlbinlog 查看binlog时报错unknown variable 'default-character-set=utf8'
    HBase Go客户端Row构造注意事项
    MySQL JOIN操作报错问题小解
    MySQL主从同步的一个小问题解决
    PHP 多个mysql连接的问题
    记一起动态库加载错误问题排查过程
    DNS缓存
    C输出大于127的ACSII字符
    Mint17 一些安装备忘
  • 原文地址:https://www.cnblogs.com/20145110tyc/p/5401735.html
Copyright © 2011-2022 走看看