1)体验敏捷开发中的两人合作。
2)进一步提高个人编程技巧与实践。
1)根据以下问题描述,练习结对编程(pair programming)实践;
2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。
3)要求在结对编程工作期间,两人的角色至少切换 4 次;
4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。
1、代码规范
1、变量名不可以是关键字,尽量做到见名知意。
2、宏定义里变量名全大写。Java包(Package)属于一种特殊情况:它们全都是小写字母,即便中间的单词亦是如此。
3、函数名采用小驼峰式命名法。
4、缩进正确
缩进是通过键盘上的 Tab 键实现的,缩进可以使程序更有层次感。原则是:如果地位相等,则不需要缩进;如果属于某一个代码的内部代码就需要缩进。
5、注释简明易懂。
(1)注释是对代码的“提示”,而不是文档。程序中的注释不可喧宾夺主,注释太多会让人眼花缭乱;
(2)如果代码本来就是清楚的,则不必加注释;
(3)边写代码边注释,修改代码的同时要修改相应的注释,以保证注释与代码的一致性,不再有用的注释要删除;
(4)当代码比较长,特别是有多重嵌套的时候,应当在段落的结束处加注释,这样便于阅读;
(5)每一条宏定义的右边必须要有注释,说明其作用。
6、下面是对类的一些建议:
(1)一个复杂的开关语句:考虑采用"多形"机制;
(2)数量众多的方法涉及到类型差别极大的操作:考虑用几个类来分别实现;
(3)许多成员变量在特征上有很大的差别:考虑使用几个类。
2、程序的总体设计

3、程序结对编程过程及功能实现情况
结对编程过程:
在此次结对编程过程中,我和元元主要使用 github 进行代码的托管,每个人利用自己的远程仓库实现和本地代码仓库的同步更新,通过fork对方的仓库进行代码的交流与更改,最后发送pull request请求,进行信息的合并,由于疫情原因我们不能当面沟通,所以我们选择了QQ通话的交流方式。


程序代码:
1 package sizeyunsuan;
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/sunxiya/sunxiya
这次的实验主旨在于体验结对编程,要求通过合作完成代码的编写,又因为疫情原因,大家不能在一起讨论,所以实验过程中还是出现了挺多的困难,但我们都一一克服了,也体会到了合作是什么意思。实验过程十分的繁琐,所以得到结果后才觉得更加开心和值得。
原文:https://www.cnblogs.com/sxyfzq/p/12637085.html