zoukankan      html  css  js  c++  java
  • 结对编程2 单元测试

    吴东益 201421123115、李志霖 201421123116
    代码地址:https://git.coding.net/w201421123115/jiedui2.git

    需求分析:

    1.通过单元测试,测试加减乘除功能。
    2.通过单元测试代码,测试计算类对于各种参数的支持:
    a. 输入是有错误的,例如 “1 ++ 2”,
    b. 在数值范围是 -1000 .. 1000 的时候,传进去 “10000 + 32768”,
    c. 或者是 “ 248 / 0” 怎么办?
    d. 怎么告诉函数的调用者 “你错了”? 把返回的字符串定义为 “-1” 来表示?
    e. 那么如果真的计算结果是 “-1” 又怎么处理呢?

    测试代码

    public class Caculation {

    public static String cacul(String s) {
    	String[] counts = null;
    	if (s.contains("+")) {
    		counts = s.split("\+");
    		// 判断是否输入正确
    		if (counts.length > 2) {
    			JOptionPane.showMessageDialog(null, "符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    			System.exit(-1); // 输入错误程序退出
    		} else
    			return caculate(counts[0], counts[1], '+');
    	} else if (s.contains("-")) {
    		counts = s.split("-");
    		// 判断是否输入正确
    		if (counts.length > 2) {
    			JOptionPane.showMessageDialog(null, "符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    			System.exit(-1); // 输入错误程序退出
    		} else
    			return caculate(counts[0], counts[1], '-');
    	} else if (s.contains("*")) {
    		counts = s.split("\*");
    		// 判断是否输入正确
    		if (counts.length > 2) {
    			JOptionPane.showMessageDialog(null, "符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    			System.exit(-1); // 输入错误程序退出
    		} else
    			return caculate(counts[0], counts[1], '*');
    	} else if (s.contains("÷")) {
    		counts = s.split("÷");
    		// 判断是否输入正确
    		if (counts.length > 2) {
    			JOptionPane.showMessageDialog(null, "符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    			System.exit(-1); // 输入错误程序退出
    		} else
    			return caculate(counts[0], counts[1], '÷');
    	}
    	if (counts.length > 2)
    		JOptionPane.showMessageDialog(null, "符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    	return "-1";
    }
    
    public static String caculate(String count_1, String count_2, char operators) {
    	switch (operators) {
    	case '+':
    		// 判断是否有分数,有则转换为Fraction对象
    		// 当第一个运算数为分数时
    		if (count_1.contains("/")) {
    			Fractions fractions1 = toFraction(count_1);
    			if (count_2.contains("/")) {
    				Fractions fractions2 = toFraction(count_2);
    				return Fractions.addtion(fractions1, fractions2);
    			} else {
    				return Fractions.addtion(fractions1, new Integer(count_2));
    			}
    		}
    		// 当第二个运算数为分数时
    		else if (count_2.contains("/")) {
    			Fractions fractions2 = toFraction(count_2);
    			return Fractions.addtion(new Integer(count_1), fractions2);
    		}
    		// 当两个整数进行运算时
    		else {
    			return new Integer(new Integer(count_1) + new Integer(count_2)).toString();
    		}
    	case '-':
    		// 当第一个运算数为分数时
    		if (count_1.contains("/")) {
    			Fractions fractions1 = toFraction(count_1);
    			if (count_2.contains("/")) {
    				Fractions fractions2 = toFraction(count_2);
    				return Fractions.subtraction(fractions1, fractions2);
    			} else {
    				return Fractions.subtraction(fractions1, new Integer(count_2));
    			}
    		}
    		// 当第二个运算数为分数时
    		else if (count_2.contains("/")) {
    			Fractions fractions2 = toFraction(count_2);
    			return Fractions.subtraction(new Integer(count_1), fractions2);
    		}
    		// 当两个整数进行运算时
    		else {
    			return new Integer(new Integer(count_1) - new Integer(count_2)).toString();
    		}
    	case '*':
    		// 当第一个运算数为分数时
    		if (count_1.contains("/")) {
    			Fractions fractions1 = toFraction(count_1);
    			if (count_2.contains("/")) {
    				Fractions fractions2 = toFraction(count_2);
    				return Fractions.multiplication(fractions1, fractions2);
    			} else {
    				return Fractions.multiplication(fractions1, new Integer(count_2));
    			}
    		}
    		// 当第二个运算数为分数时
    		else if (count_2.contains("/")) {
    			Fractions fractions2 = toFraction(count_2);
    			return Fractions.multiplication(new Integer(count_1), fractions2);
    		}
    		// 当两个整数进行运算时
    		else {
    			return new Integer(new Integer(count_1) * new Integer(count_2)).toString();
    		}
    	case '÷':
    		// 当第一个运算数为分数时
    		if (count_1.contains("/")) {
    			Fractions fractions1 = toFraction(count_1);
    			if (count_2.contains("/")) {
    				Fractions fractions2 = toFraction(count_2);
    				return Fractions.division(fractions1, fractions2);
    			} else {
    				return Fractions.division(fractions1, new Integer(count_2));
    			}
    		}
    		// 当第二个运算数为分数时
    		else if (count_2.contains("/")) {
    			Fractions fractions2 = toFraction(count_2);
    			return Fractions.division(new Integer(count_1), fractions2);
    		}
    		// 当两个整数进行运算时
    		else {
    			return Fractions.division(new Integer(count_1), new Integer(count_2));
    		}
    	}
    	return "-1";
    }
    
    public static Fractions toFraction(String s) {
    	Fractions fractions = new Fractions();
    	String[] temps = s.split("/");
    	if (temps.length > 2) {
    		JOptionPane.showMessageDialog(null, "分数符号输入错误", "错误提示", JOptionPane.PLAIN_MESSAGE);
    		System.exit(-1);
    	} else {
    		fractions.setValue(new Integer(temps[0]), new Integer(temps[1]));
    	}
    	return fractions;
    }
    

    }

    实验截图





    结对摆拍

    实验小结:

    本次实验是要我们验证自己代码是否出错,而我们对此次题意理解有一定的错误,以为是对自己输入的错误进行纠错,在逻辑上与我们的实验内容有一定的偏差,覆盖率这个重要的要求来不及达成,我们在进行进一步的调试,这次得到最大的启迪是,对于实验的要求一定要理解透彻。此次的纠错主要针对原来的代码进行对输入字符串进行判别,确定其正确性以及是什么类型的算数进行比较。

    PSP

  • 相关阅读:
    HTML引入文件的绝对路径、相对路径、根目录
    测试脚本中的等待方法:
    MVC、MTV、FBV、CBV、母版和继承:
    多窗口处理周杰伦:
    登录测试函数版:
    登录测试:
    hibernate配置详情1(hibernate.cfg.xml)
    常用数据库连接串与驱动总结
    常用数据库连接串与驱动总结
    常用数据库连接串与驱动总结
  • 原文地址:https://www.cnblogs.com/lizhilin0227/p/6637188.html
Copyright © 2011-2022 走看看