1.下载junit和hamcrest的jar包并引入项目
2.在eclipse商店中下载安装eclemma插件
3.编写判断三角形形状的代码
package lab1source; public class src1 { public static String T(double a,double b,double c){ double tem = Math.max(a, b); if(tem>c){ if(tem==a){ a = c; }else { b = c; } c = tem; } if(!(a+b>c&&Math.abs(a-b)<c)){ return "not a triangle"; }else if(a==b && a==c){ return "equilateral triangle"; }else if(a==b || a==c || b==c){ return "isosceles triangle"; }else return "scalene triangle"; } public static void main(String[] args) { System.out.println(src1.T(3, 3, 3)); } }
4.完成测试类代码
package lab1source; import static org.junit.Assert.*; import org.junit.Test; public class src1Test { src1 s1= new src1(); @Test public void testT() { assertEquals("equilateral triangle",s1.T(3,3,3)); assertEquals("isosceles triangle",s1.T(3,4,3)); assertEquals("scalene triangle",s1.T(3,4,5)); } }
5.测试结果
时间: 2024-10-11 07:39:33