• 软件工程网络15结对编程1——四则运算优化


    1.链接

    2.改进现有代码

    一、分析初始代码

    类图

    覆盖率统计




    单元测试



    所发现的不足之处

    • 类没有注释,减少易读性
    • 源程序中使用驼峰命名法,但是一些方法的命名有缺陷,如Fraction类中的creatfraction(),应改为createFraction()
    • 不能让用户选择题目的数量
    • 没有统计正确率
    • 只有两个数之间的运算

    二、功能改进与扩展

    • 为每个类增加注释,修改不恰当的命名

    • 重构Calculator类,增加calc()方法

            //Class Calculator
        	 public static String calc(String op,Fraction a,Fraction b){
        		 switch (op) {
        			case "+":
        				System.out.println("+");
        				return add(a,b);
        			case "-":
        				return sub(a,b);
        			case "×":
        				return mul(a,b);
        			case "÷":
        				return div(a,b);
        			default :
        				return null;
        			}
        	 }
      
    • 扩展了三位数之间的运算

       public static String createRandomOp(){
       	String operator = null;
       	Random random = new Random();
       	switch (random.nextInt(4)) {
       	case 0:
       		operator = "+"; 
       		break;
       	case 1:
       		operator = "-";
       		break;
       	case 2:
       		operator = "×";
       		break;
       	case 3:
       		operator = "÷";
       		break;
       	}
       	return operator;
       }   
      
       public class Createformula {
       public static int testNum = 5; 
       private String[] result = new String[testNum]; 
       private String[] number_1 = new String[testNum]; 
       private String[] number_2 = new String[testNum];
       private String[] number_3 = new String[testNum];
       private String[] operator1 = new String[testNum]; 
       private String[] operator2 = new String[testNum]; 
       private ArrayList<String> scoresList = new ArrayList<String>(); 
       private BufferedWriter writer;
      
       public Createformula() {
      
       }
       public void createTest() {
       	Random random = new Random();
       	Fraction f1 = new Fraction();
       	Fraction f2 = new Fraction();
       	Fraction f3 = new Fraction();
       	for (int i = 0; i < testNum; i++) {
       		   f1.creatfraction();
                  f2.creatfraction();
                  f3.creatfraction();
       		number_1[i] = f1.getFraction();
       		number_2[i] = f2.getFraction();
       		number_3[i] = f3.getFraction();
       		operator1[i] = createRandomOp();
       		operator2[i] = createRandomOp();
       		Fraction temp = new Fraction();  //put the result of f1f2
       	if(operator1[i].equals("+")||operator1[i].equals("-") ){
       		temp.setFraction(Calculator.calc(operator2[i],f2,f3));
       		temp.convertResult();
       		result[i] = Calculator.calc(operator1[i],f1,temp);
       	}
       	else{
       		temp.setFraction(Calculator.calc(operator1[i],f1,f2));
       		temp.convertResult();
       		result[i] = Calculator.calc(operator2[i],temp,f3);
       	}
       	}
       }  
       
        //Class Fraction
       public  void convertResult(){
       	if(fraction != null){
       	if(fraction.contains("/")){
       		String str[] = fraction.split("/");
       		this.numerator = Integer.parseInt(str[0]);
       		this.denominator = Integer.parseInt(str[1]);
       			}
       	else{
       		this.numerator = Integer.parseInt(fraction);
       		this.denominator = 1;
       	}
       }
       	else System.out.println("fraction null");
       }  
      
    • 增加用户选择题目数量功能

        //Class Background
        public void setFormulaNum() {
        	String str; 
        	str = JOptionPane.showInputDialog("请输入题目数量"); 
        	this.formulaNum = Integer.parseInt(str);
        }  
      
    • 增加正确率统计功能

        //Class Background
        public void actionPerformed(ActionEvent e) {
        		btnExit.addMouseListener(new MouseAdapter() {
        			@Override
        			public void mouseClicked(MouseEvent arg0) {
        				// TODO Auto-generated method stub
        				System.exit(0);
        			}
        		});
        		
        		btnSubmit.addMouseListener(new MouseAdapter() {
        			@Override
        			public void mouseClicked(MouseEvent arg0) {
        				// TODO Auto-generated method stub
        				isEnd=!isEnd; 
        				for (int i = 0; i < formulaNum; i++) {
        					answers[i]=tfdAnswer[i].getText();  
        				}
        				wrong= background.checkAnswer(answers);
        				String s=null;
        				if(wrong.length==0)
        					s=tips.get(5);
        				else{
        					s=tips.get(6)+"
      ";
        					String standardAnswer[]=new String[formulaNum];
        					standardAnswer=background.getStandardAnswer();
        					questions = background.getQuestions();
        					for(int i=0;i<wrong.length;i++){
        						s=s+questions[new Integer(wrong[i])-1]+standardAnswer[new Integer(wrong[i])-1];
        						s=s+"
      
      
      ";
        						}
        					s = s + tips.get(7)+
        							String.valueOf(background.getRightAnswer()*100/background.testNum) +"%";
        					}
        					
        				
        				
        				JOptionPane.showMessageDialog(null, s, "错题",JOptionPane.PLAIN_MESSAGE);;
        			}
        				
        			
        		});  
      
    • 运行结果截图:

    3.两人合作

    一、编码规范

    • 1.使用驼峰命名法
    • 2.缩进:tab
    • 3.类的开头注释

    二、结对照片

    三、码云提交记录

    四、心得体会

    结对编程确实能够带来1+1>2的效果,两个人互相讨论后各司其职,其中遇到问题互相帮助。

  • 相关阅读:
    文件上传-pubsec-文件上传大小限制
    编写 .gitignore 文件
    Git 创建点开头的文件和目录
    Git 克隆远程仓库到本地
    redis 在 windows 中的安装
    查看数据库字符集和排序规则
    centos 6 和centos 7 系统下vnc配置
    centos 6 下KVM 安装学习之旅
    Centos 下使用VLAN+Bridge 搭建KVM基础网络环境
    centos 6 KVM 网卡桥接配置
  • 原文地址:https://www.cnblogs.com/slickghost/p/8645842.html
Copyright © 2020-2023  润新知