5.2,5.3-继续完善实验3内容

实验内容:

  1 package sizeyunsuan;
  2 
  3 import static org.junit.Assert.*;
  4 
  5 import org.junit.Test;
  6 
  7 public class SiZeYunSuanTest {
  8 
  9     @Test    //test1-4为一个运算符的运算
 10     public void testTest1()throws Exception
 11     {
 12         SiZeYunSuan core=new SiZeYunSuan();
 13         int a=core.test1('-',2,8);
 14         assertEquals(-6,a);
 15         System.out.print(a);
 16         System.out.print("    减法运算");
 17         System.out.println();
 18     }
 19     @Test
 20     public void testTest2()throws Exception {
 21         SiZeYunSuan core=new SiZeYunSuan();
 22         int a=core.test1('+',3,6);
 23         assertEquals(9,a);
 24         System.out.print(a);
 25         System.out.println("    加法运算");
 26         System.out.println();
 27     }
 28     @Test
 29     public void testTest3()throws Exception {
 30         SiZeYunSuan core=new SiZeYunSuan();
 31         int a=core.test1('*',2,9);
 32         assertEquals(18,a);
 33         System.out.print(a);
 34         System.out.println("    乘法运算");
 35         System.out.println();
 36     }
 37     @Test
 38     public void testTest4() throws ChuShuWeiLingException{
 39         SiZeYunSuan core=new SiZeYunSuan();
 40         try{
 41         int a=core.test1('/',6,0);
 42         System.out.print(a);
 43         assertEquals(2,a);
 44         
 45         }catch(Exception e){}
 46         
 47         System.out.println("    除法运算");
 48         System.out.println();
 49     }
 50 
 51     @Test
 52     public void testTest5() {
 53         SiZeYunSuan core=new SiZeYunSuan();
 54         int b=core.test2(3);
 55         assertEquals(6,b);
 56         System.out.println("阶乘运算");
 57     }
 58     @Test
 59     public void testTest6() {
 60         SiZeYunSuan core=new SiZeYunSuan();
 61         int b=core.test2(4);
 62         assertEquals(24,b);
 63         System.out.println("阶乘运算2");
 64     }
 65     @Test
 66     public void testTest7()//这是两个运算符的运算
 67     {
 68         SiZeYunSuan core=new SiZeYunSuan();
 69         int a=core.test1(1,2,4,6);
 70         assertEquals(0,a);
 71         System.out.println("这是加减混合运算");
 72         int b=core.test1(2,2,4,6);
 73         assertEquals(26,b);
 74         System.out.println("这是加乘混合运算");
 75         int c=core.test1(3,2,6,6);
 76         assertEquals(3,c);
 77         System.out.println("这是加除混合运算");
 78         int d=core.test1(4,2,4,6);
 79         assertEquals(-22,d);
 80         System.out.println("这是减乘混合运算");
 81         int e=core.test1(5,2,6,6);
 82         assertEquals(1,e);
 83         System.out.println("这是减除混合运算");
 84         int f=core.test1(6,2,4,4);
 85         assertEquals(2,f);
 86         System.out.println("这是乘除混合运算");
 87     }
 88     @Test
 89     public void testTest8()//这是三个运算符的运算
 90     {
 91         SiZeYunSuan core=new SiZeYunSuan();
 92         int a=core.test1(1,2,4,6,8);
 93         assertEquals(-42,a);
 94         System.out.println("这是加减乘混合运算");
 95         int b=core.test1(2,2,4,6,6);
 96         assertEquals(5,b);
 97         System.out.println("这是加减除混合运算");
 98         
 99     }
100     @Test
101     public void testTest9()//这是四个运算符的运算
102     {
103         SiZeYunSuan core=new SiZeYunSuan();
104         int a=core.test1(1,5,4,3,2,1);
105         assertEquals(3,a);
106         System.out.println("这是加减乘除混合运算");
107         
108     }
109     
110     }
SiZeYunSuanTest
 1 package sizeyunsuan;
 2 
 3 public class SiZeYunSuan {
 4     int ranswer=0;
 5 public int test1(char op,int a,int b)throws ChuShuWeiLingException{                                                                                                   
 6     if(a>0&&b>0)
 7     {
 8         System.out.print(String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"=");
 9     }
10     else if(a<0&&b>0)
11     {
12         System.out.print("("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"=");
13     }
14     else if(a>0&&b<0)
15     {
16         System.out.print(String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"=");
17     }
18     else
19     {
20         System.out.print("("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"=");
21     }
22         switch(op) 
23         {  
24         
25         case '+':   ranswer=a+b;break;
26         case '-':   ranswer=a-b;break;
27         case '*':   ranswer=a*b;break; 
28         case '/':  try{ if(b==0){throw new ChuShuWeiLingException();}}catch(Exception e){}ranswer=a/b;break;  
29         } 
30         return ranswer;
31 }
32   
33 public int test2(int c){     
34         int jc=1;
35         for(int i=c;i>0;i--)
36         {
37             jc=jc*i;
38         }
39         ranswer=jc;
40         return ranswer;
41 }
42 
43 public int test1(int num,int a,int b,int c){                                                                                                   
44     
45     switch(num) 
46     {   
47     case 1:   ranswer=a+b-c;break;
48     case 2:   ranswer=a+b*c;break;
49     case 3:   ranswer=a+b/c;break; 
50     case 4:   ranswer=a-b*c;break;  
51     case 5:   ranswer=a-b/c;break;
52     case 6:   ranswer=a*b/c;break;
53     } 
54     return ranswer;
55 }
56 public int test1(int num,int a,int b,int c,int d){                                                                                                   
57     
58     switch(num) 
59     {   
60     case 1:   ranswer=a+b-c*d;break;
61     case 2:   ranswer=a+b-c/d;break; 
62     } 
63     return ranswer;
64 }
65 public int test1(int num,int a,int b,int c,int d,int e){                                                                                                   
66     
67     switch(num) 
68     {   
69     case 1:   ranswer=a+b-c*d/e;break; 
70     } 
71     return ranswer;
72 }
73 }
SiZeYunSuan
1 package sizeyunsuan;
2 
3 public class ChuShuWeiLingException extends Exception {
4     public ChuShuWeiLingException()
5     {
6         System.out.println("除数不能为0!");
7     }
8 
9 }
ChuShuWeiLingException

上面这部分是实验总的代码,下面各部分会显示每一个部分的伪代码!

----------------------------------------------------------------------------------------------------------------

1.开发环境---Eclipse,Eclipse-Junit4

2.人员---吕日荣(201306114315)、张梓锋(201306114318)

3.第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。并能看到代码覆盖率。

下面是加减乘除阶乘及测试的伪代码:

@Test    //test1-4为一个运算符的运算
    public void testTest1()throws Exception
    {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1('-',2,8);
        assertEquals(-6,a);
        System.out.print(a);
        System.out.print("    减法运算");
        System.out.println();
    }
    @Test
    public void testTest2()throws Exception {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1('+',3,6);
        assertEquals(9,a);
        System.out.print(a);
        System.out.println("    加法运算");
        System.out.println();
    }
    @Test
    public void testTest3()throws Exception {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1('*',2,9);
        assertEquals(18,a);
        System.out.print(a);
        System.out.println("    乘法运算");
        System.out.println();
    }
    @Test
    public void testTest4() throws ChuShuWeiLingException{
        SiZeYunSuan core=new SiZeYunSuan();
        try{
        int a=core.test1('/',6,0);
        System.out.print(a);
        assertEquals(2,a);
        
        }catch(Exception e){}
        
        System.out.println("    除法运算");
        System.out.println();
    }

    @Test
    public void testTest5() {
        SiZeYunSuan core=new SiZeYunSuan();
        int b=core.test2(3);
        assertEquals(6,b);
        System.out.println("阶乘运算");
    }
    @Test
    public void testTest6() {
        SiZeYunSuan core=new SiZeYunSuan();
        int b=core.test2(4);
        assertEquals(24,b);
        System.out.println("阶乘运算2");
    }
加减乘除阶乘的测试伪代码
int ranswer=0;
public int test1(char op,int a,int b)throws ChuShuWeiLingException{                                                                                                   
    if(a>0&&b>0)
    {
        System.out.print(String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"=");
    }
    else if(a<0&&b>0)
    {
        System.out.print("("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"=");
    }
    else if(a>0&&b<0)
    {
        System.out.print(String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"=");
    }
    else
    {
        System.out.print("("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"=");
    }
        switch(op) 
        {  
        
        case '+':   ranswer=a+b;break;
        case '-':   ranswer=a-b;break;
        case '*':   ranswer=a*b;break; 
        case '/':  try{ if(b==0){throw new ChuShuWeiLingException();}}catch(Exception e){}ranswer=a/b;break;  
        } 
        return ranswer;
}
  
public int test2(int c){     
        int jc=1;
        for(int i=c;i>0;i--)
        {
            jc=jc*i;
        }
        ranswer=jc;
        return ranswer;
}
加减乘除阶乘的核心代码

然后是两个运算符,三个运算符及四个运算符构造方法的重载

public int test1(int num,int a,int b,int c){                                                                                                   
    
    switch(num) 
    {   
    case 1:   ranswer=a+b-c;break;
    case 2:   ranswer=a+b*c;break;
    case 3:   ranswer=a+b/c;break; 
    case 4:   ranswer=a-b*c;break;  
    case 5:   ranswer=a-b/c;break;
    case 6:   ranswer=a*b/c;break;
    } 
    return ranswer;
}
public int test1(int num,int a,int b,int c,int d){                                                                                                   
    
    switch(num) 
    {   
    case 1:   ranswer=a+b-c*d;break;
    case 2:   ranswer=a+b-c/d;break; 
    } 
    return ranswer;
}
public int test1(int num,int a,int b,int c,int d,int e){                                                                                                   
    
    switch(num) 
    {   
    case 1:   ranswer=a+b-c*d/e;break; 
    } 
    return ranswer;
}
伪代码
@Test
    public void testTest7()//这是两个运算符的运算
    {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1(1,2,4,6);
        assertEquals(0,a);
        System.out.println("这是加减混合运算");
        int b=core.test1(2,2,4,6);
        assertEquals(26,b);
        System.out.println("这是加乘混合运算");
        int c=core.test1(3,2,6,6);
        assertEquals(3,c);
        System.out.println("这是加除混合运算");
        int d=core.test1(4,2,4,6);
        assertEquals(-22,d);
        System.out.println("这是减乘混合运算");
        int e=core.test1(5,2,6,6);
        assertEquals(1,e);
        System.out.println("这是减除混合运算");
        int f=core.test1(6,2,4,4);
        assertEquals(2,f);
        System.out.println("这是乘除混合运算");
    }
    @Test
    public void testTest8()//这是三个运算符的运算
    {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1(1,2,4,6,8);
        assertEquals(-42,a);
        System.out.println("这是加减乘混合运算");
        int b=core.test1(2,2,4,6,6);
        assertEquals(5,b);
        System.out.println("这是加减除混合运算");
        
    }
    @Test
    public void testTest9()//这是四个运算符的运算
    {
        SiZeYunSuan core=new SiZeYunSuan();
        int a=core.test1(1,5,4,3,2,1);
        assertEquals(3,a);
        System.out.println("这是加减乘除混合运算");
        
    }
    
测试伪代码

----------------------------------------------------------------------------------------------------

4.第三阶段目标 - 通过测试程序和API 接口测试对于各种参数的支持。并能看到代码覆盖率。

其中我们解决了除0错误。

通过这些代码我们能够解决除0错误

5.心得:通过这个实验,我对这个测试功能有了更深的了解,我和我的小伙伴在工作过程中都有互相的指点,在这个过程中得到了进步。这是一个新的内容,以前我们并没有考虑这这个测试功能,或者说我们并没有意识到我们的代码要进行测试。而这是不对的。通过这个实验,我觉得对于以后的编程会有一个新的突破。因为有了测试功能,我们的代码的容错率就有了大大的提升,当我们遇到错误的时候,也更清楚如何解决问题了。

posted @ 2015-05-08 18:34  15吕日荣  阅读(173)  评论(1编辑  收藏  举报