练习5.1——四则运算 测试与封装(更新版)
2015-05-07 18:01
645 查看
主函数:
View Code
截图:
总结:
更新处理了一些奇怪的异常(操作数下标填了个5)。。以及除数为零异常。
用户输入部分未能封装成一个类,但异常处理都有。尝试过,但是出错很多。还没能彻底实现。在总的程序来看,调用Core类时其实是不会出现除数为零或者其他异常的。
package h1; import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Test; import Excep.YichangException; import h2.Core; public class CoreTest { @Test public void testAdd() {// 加法 int n = 3;// 操作数个数 int[] num1 = { 1, 2, 3 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 1, 1 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); float r = c.getResult(); System.out.println(r); assertEquals(6, r, 0.1); } @Test public void testJian() {// 减法 int n = 3;// 操作数个数 int[] num1 = { 1, 2, 3 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 2, 2 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); float r = c.getResult(); System.out.println(r); assertEquals(-4, r, 0.1); } @Test public void testMulti() {// 乘法 int n = 3;// 操作数个数 int[] num1 = { 2, 2, 3 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 3, 3 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); float r = c.getResult(); System.out.println(r); assertEquals(12, r, 0.1); } @Test public void testDiv() {// 除法 int n = 2;// 操作数个数 int[] num1 = { 2, 4 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 4 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); float r = c.getResult(); System.out.println(r); assertEquals(0.5, r, 0.1); } @Test public void testNormal() {// 混合运算 int n = 4;// 操作数个数 int[] num1 = { 1, 2, 3, 2 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 1, 3, 4 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); float r = c.getResult(); System.out.println(r); assertEquals(4, r, 0.1); } @Test(expected = ArithmeticException.class) public void testZero() {// 除以零 int n = 3;// 操作数个数 int[] num1 = { 1, 0, 3 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 4, 1 };// 操作符下标 Core c = new Core(); c.calcute(num1, op, no, n); } @Test public void dealZero() {// 除以零(处理异常) int n = 3;// 操作数个数 int[] num1 = { 1, 0, 3 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 4, 1 };// 操作符下标 Core c = new Core(); try { c.calcute(num1, op, no, n);// 运算1/0+3 } catch (Exception e) { System.out.println("除数不能为0"); } } @Test public void testUnnormal2() {// 非法输入 int n = 4;// 操作数个数 int[] num1 = { 1, 2, 3, 2 };// 操作数 char[] op = { ' ', '+', '-', '*', '/' };// 操作符 int[] no = { 1, 3, 5};// 操作符下标--------越界 Core c = new Core(); try { c.calcute(num1, op, no, n);// 运算1+2*3/2 float r = c.getResult(); System.out.println(r); } catch (Exception e) { System.out.println("下标越界,操作符下标越界"); } } }
View Code
截图:
总结:
更新处理了一些奇怪的异常(操作数下标填了个5)。。以及除数为零异常。
用户输入部分未能封装成一个类,但异常处理都有。尝试过,但是出错很多。还没能彻底实现。在总的程序来看,调用Core类时其实是不会出现除数为零或者其他异常的。
相关文章推荐
- 练习5.1更新——四则运算 测试与封装
- 练习5.1更新——四则运算 测试与封装
- 练习5.1——四则运算 测试与封装
- 作业5 四则运算 测试与封装 5.1 5.2
- 20150421 作业5 四则运算 测试与封装 5.1 5.2(doing)
- 【作业报告】作业5 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1
- 四则运算 测试与封装5.1
- 作业5 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1
- 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1
- 【作业报告】作业5 四则运算 测试与封装 5.1 改进版
- 作业5 四则运算 测试与封装 5.1
- 四则运算 测试与封装 5.1结队
- 四则运算 测试与封装 5.1
- 20150421 作业5 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1
- 作业5 四则运算 测试与封装 5.1