一、我的结对小伙伴是:江丹仪 http://www.cnblogs.com/JDY64/
二、运行环境:eclipse
三、 我们一起合作做了抛出异常,控制了精度是小数点后一位,以及完善了5.1的第二、三阶段,小伙伴还很仔细地看出了我代码的错误,漏了一个算法,代码如下:
1.Calculator.java:
public class Calculator {//最多三个运算符 static double answer=0.0d; static char[]fh={'+','-','*','/'}; static char fh2; static char fh3; static char fh4; static int a,b,c,d,e,h,f; static int fhnum;//符号数 static final int NUM_MAX_OPERATOR=1; // 最多3个运算符 static final int NUM_SCALE=2; // 数值范围是 -100 到 100 static final int NUM_PRECISION=3; //精度是小数点后一位 public static double Onefhnum(int a,int b,int e)throws Exception{//一个运算符的方法 fh2=fh[e]; double result = 0.0d; int precision = 0; if (NUM_MAX_OPERATOR == precision) { // 这里我可以处理最多三个运算符的字符串哦:) } else if (NUM_SCALE == precision) { // 这里我可以处理数值范围是 -100 到 100的字符串哦:) } else if (NUM_PRECISION == precision) { // 这里我可以处理精度是小数点后一位的字符串哦:) } if(fh2=='+'){ answer=a+b; } if(fh2=='-'){ answer=a-b; } if(fh2=='*'){ answer=a*b; } if(fh2=='/'){ if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b; } return answer; } public static double Twofhnum(int a,int b,int c,int e,int h)throws Exception{//两个运算符的方法 double result = 0.0d; int precision = 0; if (NUM_MAX_OPERATOR == precision) { // 这里我可以处理最多三个运算符的字符串哦:) } else if (NUM_SCALE == precision) { // 这里我可以处理数值范围是 -100 到 100的字符串哦:) } else if (NUM_PRECISION == precision) { // 这里我可以处理精度是小数点后一位的字符串哦:) } fh2=fh[e]; fh3=fh[h]; if(fh2=='+') { if(fh3=='+'){ answer=a+b+c; } if(fh3=='-'){ answer=a+b-c; } if(fh3=='*'){ answer=a+b*c; } if(fh3=='/'){ if(c==0){ throw new Exception("除数不能为0!"); } answer=a+b/c; } } if(fh2=='-') { if(fh3=='+'){ answer=a-b+c; } if(fh3=='-'){ answer=a-b-c; } if(fh3=='*'){ answer=a-b*c; } if(fh3=='/'){ if(c==0){ throw new Exception("除数不能为0!"); } answer=a-b/c; } } if(fh2=='*') { if(fh3=='+'){ answer=a*b+c; } if(fh3=='-'){ answer=a*b-c; } if(fh3=='*'){ answer=a*b*c; } if(fh3=='/'){ if(c==0){ throw new Exception("除数不能为0!"); } answer=a*b/c; } } if(fh2=='/') { if(fh3=='+'){ if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b+c; } if(fh3=='-'){ if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b-c; } if(fh3=='*'){ if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b*c; } if(fh3=='/'){ if(b==0){ throw new Exception("除数不能为0!"); } answer=a-b/c; } } return answer; } public static double Threefhnum(int a,int b,int c,int d,int e,int h,int f)throws Exception{//三个运算符的方法 double result = 0.0d; int precision = 0; if (NUM_MAX_OPERATOR == precision) { // 这里我可以处理最多三个运算符的字符串哦:) } else if (NUM_SCALE == precision) { // 这里我可以处理数值范围是 -100 到 100的字符串哦:) } else if (NUM_PRECISION == precision) { // 这里我可以处理精度是小数点后一位的字符串哦:) } fh2=fh[e]; fh3=fh[h]; fh4=fh[f]; if(fh2=='+')//第一为加 { if(fh3=='+') { if(fh4=='+') {answer=a+b+c+d;} if(fh4=='-') {answer=a+b+c-d;} if(fh4=='*') {answer=a+b+c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a+b+c/d; } } if(fh3=='-') { if(fh4=='+') {answer=a+b-c+d;} if(fh4=='-') {answer=a+b-c-d;} if(fh4=='*') {answer=a+b-c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a+b-c/d; } } if(fh3=='*') { if(fh4=='+') {answer=a+b*c+d;} if(fh4=='-') {answer=a+b*c-d;} if(fh4=='*') {answer=a+b*c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a+b*c/d; } } if(fh3=='/') { if(fh4=='+') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a+b/c+d; } if(fh4=='-') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a+b/c-d; } if(fh4=='*') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a+b/c*d; } if(fh4=='/') { if(c==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a+b/c/d; } } } if(fh2=='-')//第一为减 { if(fh3=='+') { if(fh4=='+') {answer=a-b+c+d;} if(fh4=='-') {answer=a-b+c-d;} if(fh4=='*') {answer=a-b+c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a-b+c/d; } } if(fh3=='-') { if(fh4=='+') {answer=a-b-c+d;} if(fh4=='-') {answer=a-b-c-d;} if(fh4=='*') {answer=a-b-c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a-b-c/d; } } if(fh3=='*') { if(fh4=='+') {answer=a-b*c+d;} if(fh4=='-') {answer=a-b*c-d;} if(fh4=='*') {answer=a-b*c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a-b*c/d; } } if(fh3=='/') { if(fh4=='+') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a-b/c+d; } if(fh4=='-') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a-b/c-d; } if(fh4=='*') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a-b/c*d; } if(fh4=='/') { if(c==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a-b/c/d; } } } if(fh2=='*')//第一为乘 { if(fh3=='+') { if(fh4=='+') {answer=a*b+c+d;} if(fh4=='-') {answer=a*b+c-d;} if(fh4=='*') {answer=a*b+c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a*b+c/d; } } if(fh3=='-') { if(fh4=='+') {answer=a*b-c+d;} if(fh4=='-') {answer=a*b-c-d;} if(fh4=='*') {answer=a*b-c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a*b-c/d; } } if(fh3=='*') { if(fh4=='+') {answer=a*b*c+d;} if(fh4=='-') {answer=a*b*c-d;} if(fh4=='*') {answer=a*b*c*d;} if(fh4=='/') { if(d==0){ throw new Exception("除数不能为0!"); } answer=a*b*c/d; } } if(fh3=='/') { if(fh4=='+') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a*b/c+d; } if(fh4=='-') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a*b/c-d; } if(fh4=='*') { if(c==0){ throw new Exception("除数不能为0!"); } answer=a*b/c*d; } if(fh4=='/') { if(c==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a*b/c/d; } } } if(fh2=='/')//第一为除 { if(fh3=='+') { if(fh4=='+') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b+c+d; } if(fh4=='-') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b+c-d; } if(fh4=='*') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b+c*d; } if(fh4=='/') { if(b==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a/b+c/d; } } if(fh3=='-') { if(fh4=='+') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b-c+d; } if(fh4=='-') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b-c-d; } if(fh4=='*') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b-c*d; } if(fh4=='/') { if(b==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a/b-c/d; } } if(fh3=='*') { if(fh4=='+') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b*c+d; } if(fh4=='-') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b*c-d; } if(fh4=='*') { if(b==0){ throw new Exception("除数不能为0!"); } answer=a/b*c*d; } if(fh4=='/') { if(b==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a/b*c/d; } } if(fh3=='/') { if(fh4=='+') { if(b==0){ throw new Exception("除数不能为0!"); } if(c==0){ throw new Exception("除数不能为0!"); } answer=a/b/c+d; } if(fh4=='-') { if(b==0){ throw new Exception("除数不能为0!"); } if(c==0){ throw new Exception("除数不能为0!"); } answer=a/b/c-d; } if(fh4=='*') { if(b==0){ throw new Exception("除数不能为0!"); } if(c==0){ throw new Exception("除数不能为0!"); } answer=a/b/c*d; } if(fh4=='/') { if(b==0){ throw new Exception("除数不能为0!"); } if(c==0){ throw new Exception("除数不能为0!"); } if(d==0){ throw new Exception("除数不能为0!"); } answer=a/b/c/d; } } } return answer; } public static Object getAnswer() { return answer; } }
2.CalculatorTest.java:
import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class CalculatorTest { double res=0.0d; @Before public void setUp() throws Exception { } @Test public void testOnefhnum() { double res=0.0d;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Onefhnum(1,1,3); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(1.0, Calculator.getAnswer()); } @Test public void testTwofhnum() { double res= 0.0d;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Twofhnum(2,3,9,0,0); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(14.0, Calculator.getAnswer()); } @Test public void testThreefhnum() { double res=0;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Threefhnum(4,5,5,4,0,0,1); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(10.0,Calculator.getAnswer()); } }
四、运行结果:
1.以上代码的运行结果为:
2.设定了自定义抛出异常后:
除数若为0时,测试代码为:
import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class CalculatorTest { double res=0.0d; @Before public void setUp()throws Exception { } @Test public void testOnefhnum() { double res=0.0d;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Onefhnum(1,0,3); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(1.0, Calculator.getAnswer()); } @Test public void testTwofhnum() { double res= 0.0d;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Twofhnum(2,3,0,0,3); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(14.0,Calculator.getAnswer()); } @Test public void testThreefhnum() { double res=0;// 这个方法中我可以计算两个数的+ - * / try { res=Calculator.Threefhnum(4,5,5,0,0,0,3); } catch (Exception e) { e.printStackTrace(); Assert.fail("抛出异常,测试失败!"); } Assert.assertEquals(10.0,Calculator.getAnswer()); } }