编程语言
首页 > 编程语言> > 2018-2019-2 20175311 实验二 《Java开发环境的熟悉》实验报告

2018-2019-2 20175311 实验二 《Java开发环境的熟悉》实验报告

作者:互联网

2018-2019-2 20175303 实验二 《Java开发环境的熟悉》实验报告

一、实验准备

1.了解掌握实验所要用到的三种代码
伪代码
产品代码
测试代码
2.IDEA中配置单元测试工具JUnit
进入IDEA,点击File->Setting
选择Plugins,点击Marketplace查找junit
选择JUnitGenerator V2.0,点击Install

二、实验内容

任务一
对MyUtil类进行测试,测试用例至少要包含正常情况,错误情况,边界情况的测试,提交都通过的截图,截图上要有画图加水印,输入自己的学号。
任务二:
以TDD的方式研究学习StringBuffer,提交单元测试用例和测试通过的截图,截图要加上学号水印。
任务三:

任务四:
以TDD的方式开发一个复数类Complex,要求如下:

// 定义属性并生成getter,setter
double RealPart;
double ImagePart;
// 定义构造函数
public Complex()
public Complex(double R,double I)

//Override Object
public boolean equals(Object obj)
public String toString()

// 定义公有方法:加减乘除
Complex ComplexAdd(Complex a)
Complex ComplexSub(Complex a)
Complex ComplexMulti(Complex a)
Complex ComplexDiv(Complex a)

任务五:

百分制转五分制:
   如果成绩小于60,转成“不及格”
   如果成绩在60与70之间,转成“及格”
   如果成绩在70与80之间,转成“中等”
   如果成绩在80与90之间,转成“良好”
   如果成绩在90与100之间,转成“优秀”
   其他,转成“错误”

待测试代码:

        public class MyUtil{
        public static String percentage2fivegrade(int grade){
            //如果成绩小于0,转成“错误”
            if ((grade < 0))
                return "错误";
                //如果成绩小于60,转成“不及格”
            else if (grade < 60)
                return "不及格";
                //如果成绩在60与70之间,转成“及格”
            else if (grade < 70)
                return "及格";
                //如果成绩在70与80之间,转成“中等”
            else if (grade < 80)
                return "中等";
                //如果成绩在80与90之间,转成“良好”
            else if (grade < 90)
                return "良好";
                //如果成绩在90与100之间,转成“优秀”
            else if (grade <=100)
                return "优秀";
                //如果成绩大于100,转成“错误”
            else
                return "错误";
        }
    }

测试代码:

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));
    }
    @Test
    public void testExceptions() {
        assertEquals("错误",MyUtil.percentage2fivegrade(-80));
        assertEquals("错误",MyUtil.percentage2fivegrade(125));
    }
    @Test
    public void testBoundary() {
        assertEquals("不及格",MyUtil.percentage2fivegrade(0));
        assertEquals("及格",MyUtil.percentage2fivegrade(60));
        assertEquals("中等",MyUtil.percentage2fivegrade(70));
        assertEquals("良好",MyUtil.percentage2fivegrade(80));
        assertEquals("优秀",MyUtil.percentage2fivegrade(90));
        assertEquals("优秀",MyUtil.percentage2fivegrade(100));
    }
}

任务二

以TDD的方式研究学习StringBuffer:
1.TDD的测试步骤如下:
•明确当前要完成的功能,记录成一个测试列表
•快速完成编写针对此功能的测试用例
•测试代码编译不通过(没产品代码呢)
•编写产品代码
•测试通过
•对代码进行重构,并保证测试通过(重构下次实验练习)
•循环完成所有功能的开发
老师给出的代码:

public class StringBufferDemo{        
   public static void main(String [] args){    
      StringBuffer buffer = new StringBuffer();    
       buffer.append('S');     
       buffer.append("tringBuffer");     
       System.out.println(buffer.charAt(1));     
       System.out.println(buffer.capacity());     
       System.out.println(buffer.indexOf("tring"));    
       System.out.println("buffer = " + buffer.toString());    
  }    
}    

测试代码

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class StringBufferDemoTest {
    StringBuffer str12 = new StringBuffer("hereare12chr");
    StringBuffer str24 = new StringBuffer("Here are 24 characters!!");
    StringBuffer str36 = new StringBuffer("Surprise!!! Here are 36 characters!!");

    @Test
    public void testcharAt(){
        Assert.assertEquals('h', str12.charAt(0));
        Assert.assertEquals('a', str24.charAt(5));
    }

    @Rule
    public ExpectedException thrown= ExpectedException.none();
    @Test
    public void testcharAtException(){
        thrown.expect(StringIndexOutOfBoundsException.class);
        str36.charAt(40);
    }

    @Test
    public void testlength(){
        Assert.assertEquals(12, str12.length());
        Assert.assertEquals(24, str24.length());
        Assert.assertEquals(36, str36.length());
    }

    @Test
    public void testcapacity(){
        Assert.assertEquals(28, str12.capacity());
        Assert.assertEquals(40, str24.capacity());
        Assert.assertEquals(52, str36.capacity());
        Assert.assertEquals(82,str24.append("here are 22 characters").capacity());
    }

    @Test
    public void testindex(){
        Assert.assertEquals(4, str12.indexOf("a"));
        Assert.assertEquals(9, str24.indexOf("24"));
        Assert.assertEquals(-1, str36.indexOf("?"));
    }
}

任务三

让系统支持Double类,并在MyDoc类中添加测试代码表明添加正确,提交测试代码和运行结的截图,加上学号水印

任务四

以TDD的方式开发一个复数类Complex

public class myComplex {
        // 定义属性并生成getter,setter
        double RealPart;
        double ImagePart;
        // 定义构造函数
        myComplex(double R,double I){
            RealPart=R;
            ImagePart=I;
        }
        // 定义公有方法:加减乘除
        myComplex ComplexAdd(myComplex a){
            return new myComplex(RealPart+a.RealPart,ImagePart+a.ImagePart);
        }
        myComplex ComplexSub(myComplex a){
            return new myComplex(RealPart-a.RealPart,ImagePart-a.ImagePart);
        }
       myComplex ComplexMulti(myComplex a){
            return new myComplex(RealPart*a.RealPart-ImagePart*a.ImagePart,RealPart*a.ImagePart+a.RealPart*ImagePart);
        }
        myComplex ComplexDiv(myComplex a){
            return new myComplex((RealPart*a.RealPart+ImagePart*a.ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart),(-RealPart*a.ImagePart+a.RealPart*ImagePart)/(a.RealPart*a.RealPart+a.ImagePart*a.ImagePart));
        }
    //Override Object
     public boolean equals(Object obj){
            if(obj==null)
         return false;
     else {
         myComplex m = (myComplex) obj;
         if (m.RealPart == RealPart && m.ImagePart == ImagePart)
             return true;
         else return false;
     }
        }


    public String toString(){
        String output;
        if(ImagePart==0){
            return output=RealPart+"";
        }
        else if(RealPart==0 && ImagePart!=0){
            return output=ImagePart+"i";
        }
        else if(ImagePart>0) {
            return output=RealPart+"+"+ImagePart+"i";
        }
        else return output=RealPart+""+ImagePart+"i";
    }
    }

测试代码:

import junit.framework.TestCase;
import org.junit.Test;

public class myComplexTest extends TestCase {
    myComplex a = new myComplex(6,2);
    myComplex b = new myComplex(5,1);
    myComplex c = new myComplex(4,-2);
    myComplex d = new myComplex(3,1);
    myComplex e = new myComplex(6,2);
    //myComplex e = new myComplex(2,4);
    @Test
    public void testEquals() {
        assertEquals(false,a.equals(b));
        assertEquals(true,a.equals(e));
    }
    @Test
    public void testComplexAdd() throws Exception {
        assertEquals("11.0+3.0i",b.ComplexAdd(a).toString());
        assertEquals("10.0",a.ComplexAdd(c).toString());
        assertEquals("9.0-1.0i",b.ComplexAdd(c).toString());
    }
    @Test
    public void testComplexSub() {
        assertEquals("-1.0-1.0i",b.ComplexSub(a).toString());
        assertEquals("2.0+4.0i",a.ComplexSub(c).toString());
        assertEquals("2.0",b.ComplexSub(d).toString());

    }
    @Test
    public void testComplexMulti() {
        assertEquals("28.0+16.0i",b.ComplexMulti(a).toString());
        assertEquals("28.0-4.0i",a.ComplexMulti(c).toString());
        assertEquals("14.0+8.0i",b.ComplexMulti(d).toString());
    }
    @Test
    public void testComplexDiv() {
        assertEquals("2.0",a.ComplexDiv(d).toString());
        assertEquals("1.0+1.0i",a.ComplexDiv(c).toString());
        }
}

标签:Java,assertEquals,ImagePart,测试代码,2018,20175311,RealPart,public,myComplex
来源: https://www.cnblogs.com/hujidong42/p/10734956.html