团队项目一
团队项目“四则运算'部分代码及测试
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.人员---潘广玫、孙晶
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错误