{
this.lborderA = lborderA; this.lborderB = lborderB; this.lborderC = lborderC; } /*
* 判断是否是三角形 */
public boolean isTriangle(Triangle triangle) { boolean isTriangle = false;
// 判断边界,大于0 小于200,出界返回false if ((triangle.lborderA > 0 && triangle.lborderA <200) && (triangle.lborderB > 0 && triangle.lborderB <200) && (triangle.lborderC > 0 && triangle.lborderC <200)) {
// 判断两边之和大于第三边
if ((triangle.lborderA < (triangle.lborderB + triangle.lborderC)) && (triangle.lborderB < (triangle.lborderA + triangle.lborderC)) && (triangle.lborderC < (triangle.lborderA + triangle.lborderB))) { isTriangle = true; } Return isTrue; } /**
* 判断三角形类型 *
public String getType(Triangle triangle) { String strType = \ // 判断是否是三角形 if (this.isTriangle(triangle))
{
// 判断是否是等边三角形
if (triangle.lborderA == triangle.lborderB
&& triangle.lborderB == triangle.lborderC) strType = \等边三角形\ // 判断是否是不等边三角形
else if ((triangle.lborderA != triangle.lborderB) && (triangle.lborderB != triangle.lborderC) && (triangle.lborderA != triangle.lborderC)) strType = \不等边三角形\ else
strType = \等腰三角形\ }
return strType;
25
} }
解:具体步骤为:
1)新建sample 项目,源文件夹为src; 2)新建并实现Triangle 类(代码如题),包名为com.ime.sample; 3)新建源文件夹test;
4)新建并实现TriangleTest 类(代码如下),包名为com.ime.sample.test; 5)使用JUnit 运行TriangleTest。
TriangleTest类:
import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /**
* Tests about Triangle
class. *
* @author yangyz@NEUSOFT */
public class TriangleTest extends TestCase { public Triangle triangle = null; public static Test suite() {
return new TestSuite(TriangleTest.class); }
/**
* Test for Triangle#isTriangle().
*
* Setup: lborderA = 0; lborderB = 3; lborderC = 3
* Expected: method returns false */
public void testIsTriangle01() { // setup
triangle = new Triangle(0, 3, 3); // expected
boolean expected = false; // execute
boolean result = Triangle.isTriangle(triangle); // actual
boolean actual = result; // assert
assertEquals(\
}
26
/**
* Test for Triangle#isTriangle().
*
* Setup: lborderA = 3; lborderB = 3; lborderC = 3
* Expected: method returns true */
public void testIsTriangle02() { // setup
triangle = new Triangle(3, 3, 3); // expected
boolean expected = true; // execute
boolean result = Triangle.isTriangle(triangle); // actual
boolean actual = result; // assert
assertEquals(\
}
/**
* Test for Triangle#getType().
*
* Setup: lborderA = 0; lborderB = 3; lborderC = 3
* Expected: method returns \不是三角形\ */
public void testGetType01() { // setup
triangle = new Triangle(0, 1, 1); // expected
String expected = \不是三角形\ // execute
String result = Triangle.getType(triangle); // actual
String actual = result; // assert
assertEquals(\
}
/**
* Test for Triangle#getType().
*
* Setup: lborderA = 3; lborderB = 4; lborderC = 5
* Expected: method returns \不等边三角形\
27
*/
public void testGetType02() { // setup
triangle = new Triangle(3, 4, 5); // expected
String expected = \不等边三角形\ // execute
String result = Triangle.getType(triangle); // actual
String actual = result; // assert
assertEquals(\
}
/**
* Test for Triangle#getType().
*
* Setup: lborderA = 3; lborderB = 3; lborderC = 3
* Expected: method returns \等边三角形\ */
public void testGetType03() { // setup
triangle = new Triangle(3, 3, 3); // expected
String expected = \等边三角形\ // execute
String result = Triangle.getType(triangle); // actual
String actual = result; // assert
assertEquals(\
}
/**
* Test for Triangle#getType().
*
* Setup: lborderA = 3; lborderB = 3; lborderC = 5
* Expected: method returns \等腰三角形\ */
public void testGetType04() { // setup
triangle = new Triangle(3, 3, 5); // expected
String expected = \等腰三角形\
28
// execute
String result = Triangle.getType(triangle); // actual
String actual = result; // assert
assertEquals(\ }
// 执行测试
public static void main(String[] args) { // Text ui 方式
// junit.textui.TestRunner.run(suite()); // Swing ui 方式
junit.swingui.TestRunner.run(TriangleTest.class); // AWT ui 方式
// junit.awtui.TestRunner.run(TriangleTest.class); } }
29