标签:
Tasks:
1. Install Junit(4.12), Hamcrest(1.3) with Eclipse
将两个jar包添加到工程中
2. Install Eclemma with Eclipse
3. Write a java program for the triangle problem and test the program with Junit.
[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.
运行截图如下:
代码如下:
package TestTriangle; public class triangles { public static String triangleshape(int a,int b, int c){ if(a == b && a == c && b == c){ return "equilateral"; } else if(a == b || a == c || b == c){ return "isosceles"; } else{ return "scalene"; } } }
package TestTriangle; 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 testTriangles { private int a; private int b; private int c; private String expected; private String result = null; public testTriangles(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[][]{ {1,1,1,"equilateral"}, {2,3,4,"scalene"}, {3,5,5,"isosceles"}, {6,6,8,"isosceles"} }); } @Test public void test() { assertEquals(this.expected,triangles.triangleshape(a,b,c)); } }
Lab 1: Write a java program for the triangle problem and test the program with Junit.
标签:
原文地址:http://www.cnblogs.com/eric7/p/5296513.html