ComputerPractice1
Tasks:
1.Install Junit(4.12), Hamcrest(1.3) with Eclipse
Answer:建立lib文件夹后,将hamcrest-core-1.3和junit-4.10拖入lib文件夹,之后为Junit(4.10), Hamcrest(1.3)分别build path。
2.Install Eclemma with Eclipse
Answer:进入eclipse的dropins文件中,将解压后的eclemma-2.2.3复制入dropins中
之后删除eclemma-2.2.3中的META-INF文件夹(我的eclipse版本需要删除META-INF文件才可以使用eclemma),打开eclipse就可以显示图标(最左侧)即可使用。
也可以右键选择Coverage As运行。
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.
Result:
以Coverage As方式运行程序:
triangleProblem代码如下:
package ComputerPractice1;
public class triangleProblem {
public static String triangleshape(int a,int b, int c){
if(a+b>c&&a+c>b&&b+c>a){
if(a == b && a == c && b == c){
return "equilateral";
}
else if(a == b || a == c || b == c){
return "isosceles";
}
else{
return "scalene";
}
}else{
return "That is not a triangle!";
}
}
}
Test代码如下:
package ComputerPractice1;
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 testTriangle {
private int a;
private int b;
private int c;
private String expected;
private String result = null;
public testTriangle(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[][]{
{8,8,8,"equilateral"},
{2,3,4,"scalene"},
{3,8,8,"isosceles"},
{1,1,8,"That is not a triangle!"}
});
}
@Test
public void test() {
assertEquals(this.expected,triangleProblem.triangleshape(a,b,c));
}
}