实验要求:
- Install Junit(4.12), Hamcrest(1.3) with Eclipse
- Install Eclemma with Eclipse
- 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.
实验代码:
Triangle类:
package ST; public class Triangle { public String judge(double a, double b, double c) { if(a+b>c&&b+c>a&&a+c>b) { if(a==b||b==c||c==a) { if(a==b&&b==c) return "equilateral"; else return "isosceles"; } else return "scalene"; } else return "it is not a triangle"; } }
测试类:
package ST; import org.junit.Test; import org.junit.Before; import static org.junit.Assert .*; public class TriangleTest { private Triangle triangle; @Before public void setUp() throws Exception { triangle = new Triangle(); } @Test public void test() { assertEquals("isosceles", triangle.judge(4,4,5)); assertEquals("it is not a triangle", triangle.judge(1,1,5)); assertEquals("equilateral", triangle.judge(4,4,4)); assertEquals("scalene", triangle.judge(3,4,5)); } }