实验二 结对编程 第二阶段

实验二   结对编程

一、实验目标

  1)体验敏捷开发中的两人合作。

  2)进一步提高个人编程技巧与实践。

二 、实验内容

  1)根据以下问题描述,练习结对编程(pair programming)实践;

  2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。

  3)要求在结对编程工作期间,两人的角色至少切换 4 次;

  4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。

三、实验过程

1、代码规范

(1)类名首字母应该大写。字段、方法以及对象(句柄)的首字母应小写。对于所有标识符,其中包含的所有
单词都应紧靠在一起,而且大写中间单词的首字母。若在定义中出现了常数初始化字符,则大写static final基
本类型标识符中的所有字母。这样便可标志出它们属于编译期的常数。Java包(Package)属于一种特殊情况:
它们全都是小写字母,即便中间的单词亦是如此。
(2)为了常规用途而创建一个类时,请采取"经典形式",并包含对下述元素的定义:equals()
hashCode()
toString()
clone()(implement Cloneable)
implement Serializable
(3)对于自己创建的每一个类,都考虑置入一个main(),其中包含了用于测试那个类的代码。为使用一个项目中的
类,我们没必要删除测试代码。若进行了任何形式的改动,可方便地返回测试。这些代码也可作为如何使用类的
一个示例使用。
(4)应将方法设计成简要的、功能性单元,用它描述和实现一个不连续的类接口部分。理想情况下,方法应简明扼
要。若长度很大,可考虑通过某种方式将其分割成较短的几个方法。这样做也便于类内代码的重复使用(有些时候,
方法必须非常大,但它们仍应只做同样的一件事情)。
(5)设计一个类时,请设身处地为客户程序员考虑一下(类的使用方法应该是非常明确的)。然后,再设身处地为管
理代码的人考虑一下(预计有可能进行哪些形式的修改,想想用什么方法可把它们变得更简单)。
(6)使类尽可能短小精悍,而且只解决一个特定的问题。下面是对类设计的一些建议:
一个复杂的开关语句:考虑采用"多形"机制
数量众多的方法涉及到类型差别极大的操作:考虑用几个类来分别实现
许多成员变量在特征上有很大的差别:考虑使用几个类
(7)让一切东西都尽可能地"私有"-private。可使库的某一部分"公共化"(一个方法、类或者一个字段等等),就永远
不能把它拿出。若强行拿出,就可能破坏其他人现有的代码,使他们不得不重新编写和设计。若只公布自己必须公布
的,就可放心大胆地改变其他任何东西。在多线程环境中,隐私是特别重要的一个因素-只有private字段才能在非同
步使用的情况下受到保护。
(8)谨惕"巨大对象综合症。对一些习惯于顺序编程思维、且初涉OOP领域的新手,往往喜欢先写一个顺序执行的程序,
再把它嵌入一个或两个巨大的对象里。根据编程原理,对象表达的应该是应用程序的概念。

2、程序的总体设计

3、程序结对编程过程及功能实现情况

结对编程过程:

在此次结对编程过程中,我和我的伙伴主要使用 github 进行代码的托管,每个人利用自己的远程仓库实现和本地代码仓库

的同步更新,然后通过fork对方的仓库进行代码的交流与更改,然后发送pull request请求,进行信息的合并,由于不在一起

开发,我们的选择了QQ通话的交流方式。

程序代码:

  1 package sizeyusuan;
  2 import java.util.*;
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.File;
  6 import java.io.FileOutputStream;
  7 import java.io.FileReader;
  8 import java.io.FileWriter;
  9 import java.io.IOException;
 10 import java.io.OutputStreamWriter;
 11 import java.io.PrintWriter;
 12 import java.io.RandomAccessFile; 
 13 public class xiaoxuesheng {
 14     private static Random random = new Random();
 15     public static int range;
 16     public static String reductionofFraction(int a, int b) {
 17         int y = 1;
 18         for (int i = a; i >= 1; i--) {
 19             if (a % i == 0 && b % i == 0) {
 20                 y = i;
 21                 break;
 22             }
 23         }
 24         int z = a / y;// 分子
 25         int m = b / y;// 分母
 26         if (z == 0) {
 27             return "0";
 28         }
 29         if(m==1) return z+"";
 30         else  return biaodashi(z,m);
 31         
 32     }
 33     public static String biaodashi(int a,int b) {
 34             int c;
 35             c=a/b;
 36             int d;
 37             d=a%b; 
 38             {if(d==0) {return c+"";}
 39             return c+"'"+d+"/"+b;}
 40         }return a+"/"+b;
 41     }
 42     
 43 public static void main(String[] args){
 44      Scanner sc= new Scanner(System.in);
 45      System.out.println("请输入产生几以内的数字:");
 46      range=sc.nextInt();
 47      System.out.println("请输入产生多少个运算表达式:");
 48      int num=sc.nextInt();
 49      int rightcount[]=new int[num+2];
 50      int wrongcount[]=new int[num+2];
 51      int right1=0;
 52     int wrong1=0;
 53     String[] results=new String[num];int i;
 54      for( i=0;i<num;i++){ 
 55      
 56         String expArr[]=new String[2];//定义生成的题目
 57         int a= (int) (random.nextInt(range));//分子
 58         int b= (int) (random.nextInt(range));//分母
 59         int c= (int) (random.nextInt(range));//另一个分子
 60         int d= (int) (random.nextInt(range));//另一个分母
 61         int fuhao;//运算符
 62         fuhao= (int) (random.nextInt(4));
 63         if(b!=0&&d!=0) {//分母均不为0时生成带有分数的计算题,同时计算结果
 64         if(fuhao==0) {
 65             int fenzi=a*d+b*c;
 66             int fenmu=b*d;
 67             expArr[0]=biaodashi(a,b)+'+'+biaodashi(c,d)+'=';
 68             System.out.println(expArr[0]);
 69             results[i]=reductionofFraction(fenzi, fenmu);
 70         
 71         }
 72         if(fuhao==1&&a*d-b*c>=0) {
 73             int fenzi=a*d-b*c;
 74             int fenmu=b*d;
 75             expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
 76             System.out.println(expArr[0]);
 77             results[i]=reductionofFraction(fenzi, fenmu);
 78             
 79         }
 80         if(fuhao==1&&a*d-b*c<0) {
 81             int fenzi=b*c-a*d;
 82             int fenmu=b*d;
 83             expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
 84             System.out.println(expArr[0]);
 85             results[i]=reductionofFraction(fenzi, fenmu);
 86             
 87         }
 88         if(fuhao==2) {
 89             int fenzi=a*c;
 90             int fenmu=b*d;
 91             expArr[0]=biaodashi(a,b)+'×'+biaodashi(c,d)+'=';
 92             System.out.println(expArr[0]);
 93             results[i]=reductionofFraction(fenzi, fenmu);
 94             
 95         }
 96         if(fuhao==3&&c!=0) {
 97             int fenzi=a*d;
 98             int fenmu=b*c;
 99             expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
100             System.out.println(expArr[0]);
101             results[i]=reductionofFraction(fenzi, fenmu);
102             
103         }
104         if(fuhao==3&&c==0) {
105             break;
106             /*c=1;
107             int fenzi=a*d;
108             int fenmu=b*c;
109             expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
110             System.out.println(expArr[0]);
111             results[i]=reductionofFraction(fenzi, fenmu);*/
112             
113         }
114         
115         }
116         else {//分母至少一个为0时生成只含有整式的运算式,同时计算结果
117             b=1; d=1;
118             if(fuhao==0) {
119             int fenzi=a*d+b*c;
120             int fenmu=b*d;
121             expArr[0]=a+"+"+c+"=";
122             System.out.println(expArr[0]);
123             results[i]=reductionofFraction(fenzi, fenmu);
124             
125         }
126         if(fuhao==1&&a*d-b*c>=0) {
127             int fenzi=a*d-b*c;
128             int fenmu=b*d;
129             expArr[0]=a+"-"+c+"=";
130             System.out.println(expArr[0]);
131             results[i]=reductionofFraction(fenzi, fenmu);
132             
133         }
134         if(fuhao==1&&a*d-b*c<0) {
135             int fenzi=b*c-a*d;
136             int fenmu=b*d;
137             expArr[0]=c+"-"+a+"=";
138             System.out.println(expArr[0]);
139             results[i]=reductionofFraction(fenzi, fenmu);
140             
141         }
142         if(fuhao==2) {
143             int fenzi=a*c;
144             int fenmu=b*d;
145             expArr[0]=c+"×"+a+"=";
146             System.out.println(expArr[0]);
147             results[i]=reductionofFraction(fenzi, fenmu);
148             
149         }
150         if(fuhao==3&&c!=0) {
151             int fenzi=a*d;
152             int fenmu=b*c;
153             expArr[0]=a+"÷"+c+"=";
154             System.out.println(expArr[0]);
155             results[i]=reductionofFraction(fenzi, fenmu);
156             
157         }
158         if(fuhao==3&&c==0) {
159             break;
160             /*c=1;
161             int fenzi=a*d;
162             int fenmu=b*c;
163             expArr[0]=a+"÷"+c+"=";
164             System.out.println(expArr[0]);
165             results[i]=reductionofFraction(fenzi, fenmu);*/
166             
167         }
168         
169         }   
170      FileWriter fw = null;
171     try {
172    
173         File f=new File("Exersies.txt");//题目写入
174         fw = new FileWriter(f, true);
175     } catch (IOException e) {
176         e.printStackTrace();
177     }if(expArr[0]!=null) {
178     PrintWriter pw = new PrintWriter(fw);
179     pw.println(i+1+"."+expArr[0]);
180     pw.flush();
181     try {
182         fw.flush();
183         pw.close();
184         fw.close();
185     } catch (IOException e) {
186         e.printStackTrace();
187     }}FileWriter fn = null;
188     try {
189         
190             File f=new File("Answer.txt");//答案写入
191             fn = new FileWriter(f, true);
192         } catch (IOException e) {
193             e.printStackTrace();
194         }if(expArr[0]!=null) {
195         PrintWriter pn = new PrintWriter(fn);
196         pn.println(i+1+"."+results[i]);
197         pn.flush();
198         try {
199             fn.flush();
200             pn.close();
201             fn.close();
202         } catch (IOException e) {
203             e.printStackTrace();
204         }}
205     }
206      System.out.println("输入ok提交!");
207      Scanner sc1=new Scanner(System.in);
208      String submit=sc1.nextLine();
209         if(submit.equals("ok")){
210      String array[]=new String[num];
211      try
212         {   int k=0;
213             
214             FileReader fr = new FileReader("H://eclipse2//eclipse3//sizeyusuan//Your_answers.txt");
215             BufferedReader br = new BufferedReader(fr);
216             String s ;
217             while((s = br.readLine())!=null) {//读取小学生的答案
218                 array[k]=s;    k++;
219                 }br.close();
220             fr.close();        
221             }catch(IOException e){
222                 System.out.println("指定文件不存在");
223             }
224     for(int j=0;j<num;j++){
225          if(array[j].equals(results[j])) {//验证答案,统计正确和错误的个数
226              
227              rightcount[j]=j+1;
228              right1++;
229          }
230          else {
231              
232              wrongcount[j]=j+1;
233              wrong1++;
234          }
235      }
236     FileWriter fg = null;
237     try {
238         //反馈正确与错误题目的信息
239             File f=new File("Grade.txt");
240             fg = new FileWriter(f, true);
241         } catch (IOException e) {
242             e.printStackTrace();
243         }
244         PrintWriter pg = new PrintWriter(fg);
245         pg.println(" ");
246         pg.print("Correct:"+right1+"(");
247         for (int j = 0; j <= num; j++) {
248             if (rightcount[j] != 0) {
249                  pg.print(rightcount[j] + ",");
250             }
251         }
252         pg.println(")");
253         pg.print("Wrong:"+wrong1+"(");
254         for (int j = 0; j <= num; j++) {
255             if (wrongcount[j] != 0) {
256                  pg.print(wrongcount[j] + ",");
257             }
258         }
259         pg.print(")");
260         pg.flush();
261         try {
262             fg.flush();
263             pg.close();
264             fg.close();
265         } catch (IOException e) {
266             e.printStackTrace();
267         }}
268 }
269 }

 4、项目github地址

https://github.com/end57/wordcount


四、实验总结

每道题目中出现的运算符个数我们只实现了单个运算符。遇到的困难主要是数组越界,通过严谨的检查
以及步步验证的方式找出了错误并解决了。另一个须待解决的问题是生成题目的速度有些缓慢,因此完
善了多次的算法来提高代码的执行效率。在编写程序的时候,最主要的问题就是对随机出现的运算符进
行讨论,因为出现了很多种情况,在编写过程中容易出现遗漏,或者算法不正确等情况,导致最后得到
的答案不同,基本上处理方法都是调试,观察程序运行到哪步出现错误,然后对算法出错的那段代码进
行修改。
总的来说,在结对编程中我们有各自的想法,通过代码的形式来进行人与人之间的交流不失为一种好的
方法,在这个过程中我们培养了团队协作的能力和与他人交际的能力,同时也促使自身的编程能力不断
提高,这使我们都受益匪浅。当然如果还有机会的话,我还是希望可以实现带括号的运算式的生成以及
答案的计算。
原文地址:https://www.cnblogs.com/321ending/p/12625266.html