- Install Junit(4.12), Hamcrest(1.3) with Eclipse

2. Install Eclemma with Eclipse

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.
the code:
package exp1; public class Triangle { public static String Triangle (int a, int b, int c){ if(a+b > c && a+c > b && b+c > a){ if (a == b && b == c) return "equilateral triangle"; else if (a == b || b == c || c == a) return "isosceles triangle"; else return "scalene triangle"; } else return "not a triganle"; } }
the test code:
package tester; import static org.junit.Assert.*; import org.junit.Test; import exp1.Triangle; public class TestTriangle { @Test public void testTriangle() { assertEquals("not a triganle",new Triangle().Triangle(1,2,3)); assertEquals("equilateral triangle",new Triangle().Triangle(3,3,3)); assertEquals("isosceles triangle",new Triangle().Triangle(4,4,5)); assertEquals("scalene triangle",new Triangle().Triangle(2,3,4)); } }
result:

The main problem: I added yhe source of junit and hamcrest in the property in of the project, but it still didn't pass. So I used the junit in the Eclipse.
浙公网安备 33010602011771号