st第一次LAB

一、实验名称

  三角形判定程序测试

二、实验目的

  编写一个判定三角形为哪种三角形的程序,了解一个程序测试的完整过程,利用junit插件和eclemma插件对该程序进行测试。

三、实验过程

  1.安装junit、hamcrest插件

   从办公网课程讨论区下载junit.jar和hamcrest.jar。打开eclipse,新建一哥java project,在poperties的选项中添加junit.jar和hamcrest.jar,如下图所示。

    

  2.安装eclemma

   点击eclipse上方导航栏中的Help,再点击下拉菜单中的eclipse Marketplace,在该界面的find后面的输入框中输入eclemma,点击搜索,之后点击install,如下图所示

    

    

  3.编写程序和测试程序

   代码如下:

   三角形判定代码:

 1 package com.triangle;
 2 
 3 
 4 public class Triangle {
 5      public static String tri1(int a, int b, int c){
 6          if(a+b<c||a+c<b||b+c<a){
 7              return "It's not a triangle";
 8          }
 9          else if(a==b||b==c||c==a){
10              if(a==b&&b==c){
11                  return "The triangle is the equilateral";             
12              }
13              else{
14                  return "The triangle is the isosceles";
15              }
16              
17          }
18          else{
19              return "The triangle is the scalene";
20          }
21      }
22 }

 

   测试代码

 1 package com.triangle;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Assert;
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 public class tesyTriangle {
10         private Triangle tri0;
11         @Before
12         public void setUp(){
13         tri0 = new Triangle();
14         }
15         
16         @Test
17          public void testTri(){
18             assertEquals("It's not a triangle",tri0.tri1(1,2,5));
19             assertEquals("The triangle is the equilateral",tri0.tri1(6,6,6));
20             assertEquals("The triangle is the isosceles",tri0.tri1(6,6,5));
21             assertEquals("The triangle is the scalene",tri0.tri1(4,5,6));
22 
23     }
24 
25 }

四、实验结果

  

 

posted @ 2016-03-19 18:29  Chonny  阅读(150)  评论(0)    收藏  举报