ComputerPractice1

ComputerPractice1

Tasks:

1.Install Junit(4.12), Hamcrest(1.3) with Eclipse

Answer:建立lib文件夹后,将hamcrest-core-1.3和junit-4.10拖入lib文件夹,之后为Junit(4.10), Hamcrest(1.3)分别build path。

 

2.Install Eclemma with Eclipse

Answer:进入eclipse的dropins文件中,将解压后的eclemma-2.2.3复制入dropins中

 

之后删除eclemma-2.2.3中的META-INF文件夹(我的eclipse版本需要删除META-INF文件才可以使用eclemma),打开eclipse就可以显示图标(最左侧)即可使用。

 

也可以右键选择Coverage As运行。

3.Write a java program for the triangle problem and test the program with Junit.

a)     Description of triangle problem:

Function triangle takes three integers a,b,c which are length of triangle sides; calculates whether the triangle is equilateral, isosceles, or scalene.

Result:

以Coverage As方式运行程序:

 

 

triangleProblem代码如下:

package ComputerPractice1;

 

public class triangleProblem {

    public static String triangleshape(int a,int b, int c){

       if(a+b>c&&a+c>b&&b+c>a){

       if(a == b && a == c && b == c){

               return "equilateral";

           }

           else if(a == b || a == c || b == c){

               return "isosceles";

           }

           else{

               return "scalene";

           }

       }else{

       return "That is not a triangle!";

       }

    }

}

Test代码如下:

package ComputerPractice1;

import static org.junit.Assert.*;

 

import java.util.Arrays;

import java.util.Collection;

 

import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.Parameterized;

import org.junit.runners.Parameterized.Parameters;

 

@RunWith(Parameterized.class)

public class testTriangle {

        private int a;

        private int b;

        private int c;

        private String expected;

        private String result = null;

       

        public testTriangle(int a,int b, int c, String expected){

            this.a = a;

            this.b = b;

            this.c = c;

            this.expected= expected;

           

            }

       

        @Parameters

        public static Collection<Object[]> getData(){

        return Arrays.asList(new Object[][]{

        {8,8,8,"equilateral"},

        {2,3,4,"scalene"},

        {3,8,8,"isosceles"},

        {1,1,8,"That is not a triangle!"}

        });

        }

       

        @Test

        public void test() {

        assertEquals(this.expected,triangleProblem.triangleshape(a,b,c));

        }

}

 

 

转载于:https://www.cnblogs.com/wangyanchen/p/8622902.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值