一、实验目标
1)体验敏捷开发中的两人合作。
2)进一步提高个人编程技巧与实践。
二 、实验内容
1)根据以下问题描述,练习结对编程(pair programming)实践;
2)要求学生两人一组,自由组合。每组使用一台计算机,二人共同编码,完成实验要求。
3)要求在结对编程工作期间,两人的角色至少切换 4 次;
4)编程语言不限,版本不限。建议使用 Python 或 JAVA 进行编程。
三、实验过程
代码规范:
1.统一变量名,不用太难的英文,尽量用拼音。
2.尽量不用Tab键,用空格。
3.每个部分需要写注释,尽量不换行。
4.代码简练清晰,避免过长程序。
流程图:
合作:考虑到小学学了分数,所以编写程序的时候加了分数的运算,经过商量,分数有关的处理交给队友完成,我负责文件的生成、将题目等写入以及结果的部分,主要通过qq来交流。
用的仓库是队友的,README.md文件是我上传是因为他忘了写,我们最后才发觉,所以我顺便就写了。
代码:
package test;
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class test {
private static Random random = new Random();
public static int range;
public static String yuefen(int a, int b)// 分数约分,用在计算结果中
{
int y = 1;
for (int i = a; i >= 1; i--)
{
if (a % i == 0 && b % i == 0)
{
y = i;
break;
}
}
int z = a / y;// 分子
int m = b / y;// 分母
if (z == 0) {
return "0";
}
if(m==1) return z+"";
else
return biaodashi(z,m);
}
public static String biaodashi(int a,int b)//判断并将假分数化为带分数
{
if(a>=b)
{
int c;
c=a/b;
int d;
d=a%b;
{
if(d==0)
{
return c+"";
}
return c+"'"+d+"/"+b;
}
}
return a+"/"+b;
}
public static void main(String[] args)//主函数部分
{
Scanner sc= new Scanner(System.in);
System.out.println("欢迎使用本程序!");
System.out.println("本程序为随机生成小学四则运算表达式!");
System.out.println("请按任意键生成题目");
Scanner input = new Scanner(System.in);
String str = input.next();
range=100;
int num= 10;
int rightcount[]=new int[num+2];
int wrongcount[]=new int[num+2];
int gradecount[]=new int[num+2];
int right1=0;
int wrong1=0;
int grade1=0;
String[] results=new String[num];int i;
for( i=0;i
String expArr[]=new String[2];//定义生成的题目
int a= (int) (random.nextInt(range));//分子
int b= (int) (random.nextInt(range));//分母
int c= (int) (random.nextInt(range));//另一个分子
int d= (int) (random.nextInt(range));//另一个分母
int yunsuanfu;//运算符
yunsuanfu= (int) (random.nextInt(4));
if(b!=0&&d!=0) //分母均不为0时生成带有分数的计算题,同时计算结果
{
if(yunsuanfu==0)
{
int fenzi=a*d+b*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'+'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==1&&a*d-b*c>=0)
{
int fenzi=a*d-b*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==1&&a*d-b*c<0)
{
int fenzi=b*c-a*d;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'-'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==2)
{
int fenzi=a*c;
int fenmu=b*d;
expArr[0]=biaodashi(a,b)+'×'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==3&&c!=0)
{
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==3&&c==0)
{
break;
/*c=1;
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=biaodashi(a,b)+'÷'+biaodashi(c,d)+'=';
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);*/
}
}
else //分母至少一个为0时生成只含有整式的运算式,同时计算结果
{
b=1; d=1;
if(yunsuanfu==0)
{
int fenzi=a*d+b*c;
int fenmu=b*d;
expArr[0]=a+"+"+c+"=";
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==1&&a*d-b*c>=0)
{
int fenzi=a*d-b*c;
int fenmu=b*d;
expArr[0]=a+"-"+c+"=";
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==1&&a*d-b*c<0)
{
int fenzi=b*c-a*d;
int fenmu=b*d;
expArr[0]=c+"-"+a+"=";
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==2)
{
int fenzi=a*c;
int fenmu=b*d;
expArr[0]=c+"×"+a+"=";
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==3&&c!=0)
{
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=a+"÷"+c+"=";
System.out.println(expArr[0]);
results[i]=yuefen(fenzi, fenmu);
}
if(yunsuanfu==3&&c==0)
{
break;
/*c=1;
int fenzi=a*d;
int fenmu=b*c;
expArr[0]=a+"÷"+c+"=";
System.out.println(expArr[0]);
results[i]=reductionofFraction(fenzi, fenmu);*/
}
}
FileWriter fw = null;
try {
File f=new File("Exersies.txt");//题目写入
fw = new FileWriter(f, true);
} catch (IOException e)
{
e.printStackTrace();
}
if(expArr[0]!=null)
{
PrintWriter pw = new PrintWriter(fw);
pw.println(i+1+"."+expArr[0]);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
FileWriter fn = null;
try {
File f=new File("Answer.txt");//答案写入
fn = new FileWriter(f, true);
} catch (IOException e)
{
e.printStackTrace();
}
if(expArr[0]!=null)
{
PrintWriter pn = new PrintWriter(fn);
pn.println(i+1+"."+results[i]);
pn.flush();
try {
fn.flush();
pn.close();
fn.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
FileWriter fo = null;
try {
File f=new File("Your_answers.txt");//答案输入
fo = new FileWriter(f, true);
} catch (IOException e)
{
e.printStackTrace();
}
try {
fo.flush();
fo.close();
} catch (IOException e)
{
e.printStackTrace();
}
System.out.println("请在Your_answers.txt中写入答案");
System.out.println("请输入ok提交答案");
Scanner sc1=new Scanner(System.in);
String submit=sc1.nextLine();
if(submit.equals("ok"))
{
String array[]=new String[num];
try
{
int k=0;
FileReader fr = new FileReader("Your_answers.txt");
BufferedReader br = new BufferedReader(fr);
String s ;
while((s = br.readLine())!=null)//读取输入的答案
{
array[k]=s; k++;
}
br.close();
fr.close();
}catch(IOException e)
{
System.out.println("指定文件不存在");
}
for(int j=0;j
if(array[j].equals(results[j]))//验证答案,统计正确和错误的个数
{
rightcount[j]=j+1;
right1++;
grade1=right1*10;
}
else
{
wrongcount[j]=j+1;
wrong1++;
}
grade1=right1*10;
}
FileWriter fg = null;
try //反馈正确与错误题目的信息
{
File f=new File("Grade.txt");
fg = new FileWriter(f, true);
} catch (IOException e)
{
e.printStackTrace();
}
PrintWriter pg = new PrintWriter(fg);
pg.println(" ");
pg.print("Correct:"+right1+"(正确题号");
for (int j = 0; j <= num; j++) {
if (rightcount[j] != 0) {
pg.print(rightcount[j] + ",");
}
}
pg.println(")");
pg.print("Wrong:"+wrong1+"(错误题号");
for (int j = 0; j <= num; j++) {
if (wrongcount[j] != 0) {
pg.print(wrongcount[j] + ",");
}
}
pg.println(")");
pg.print("GRADE:"+grade1);
pg.flush();
try {
fg.flush();
pg.close();
fg.close();
} catch (IOException e)
{
e.printStackTrace();
}
}System.out.println("成绩已经生成请在文件中查看。");
System.out.println("感谢你的使用!");
}
}
运行结果:
博客地址:
盛宁:https://www.cnblogs.com/shengning/
罗皖瑞:https://home.cnblogs.com/u/pass/
仓库地址:https://github.com/2994226544/exm
实验总结:经过漫长的时间,终于把实验完成了,第一次进行结对编程,两个人一起编程要比一个人要麻烦的多,队友之间的合作就显得很重要了,实验过程中一个人想不通的地方两个人想也比较容易。在实验中虽然也会有想法冲突的时候,但并不是无意义的,在冲突中可能会产生更好的想法,两个人一起讨论也能有所提高。这次实验让我更加熟悉了github的相关操作。