一、上机时间及地点
2016年3月18日10:25到12:00,上机55A210
二、上机实验内容
1、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.
三、实验过程
1、导入Junit和Hamcrest的JAR包
新建Java Project
右键Project并Build Path
2、为Eclipse安装Eclemma
在help菜单中点击eclipse marketplace 并查找eclemma安装
3、编写算法
1 package moody; 2 public class Calculator { 3 private static int result = 0; 4 public void triangle(int a,int b,int c) 5 { 6 if((a+b)>c && (a+c)>b && (b+c)>a && a>0 && b>0 && c>0)//判断是否构成三角形 7 { 8 if((a==b)||(a==c)||(b==c)) 9 { 10 if((a == b)&&(a == c)) 11 { 12 result = 3;//等边 13 } 14 else 15 { 16 result = 2;//等腰 17 } 18 19 } 20 else 21 { 22 result = 1;//普通 23 } 24 } 25 26 else 27 { 28 result = 0;//不是三角形 29 } 30 31 } 32 public int getReuslt(){ 33 return result; 34 } 35 36 public void clear(){ 37 result = 0; 38 } 39 }
4、编写测试程序
1 package moody; 2 import static org.junit.Assert.*; 3 import org.junit.Test; 4 public class TestCalculator { 5 private static Calculator cal = new Calculator(); 6 @Test 7 public void testTriangle(){ 8 9 cal.triangle(2, 2, 2); 10 assertEquals(3, cal.getReuslt());//等边三角形 11 cal.triangle(3, 3, 5); 12 assertEquals(2, cal.getReuslt());//等腰三角形 13 cal.triangle(2, 3, 4); 14 assertEquals(1, cal.getReuslt());//普通三角形 15 cal.triangle(1, 2, 3); 16 assertEquals(0, cal.getReuslt());//不能构成三角形 17 cal.triangle(-1, 5, 3); 18 assertEquals(0, cal.getReuslt());//不能构成三角形 19 } 20 21 }
5、进行测试
原文:http://www.cnblogs.com/moody/p/5293006.html