结对编程1——四则运算-GUI

时间:2023-12-15 23:27:38

码市链接:https://coding.net/u/hmhhh/p/hmh-homework/git/tree/master/

201421123003 黄建英 201421123004 黄美海

题目描述:

我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序。进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac、Linux,web,手机上的),成为一个有基本功能、一定价值的程序。在下面的功能需求中实现两个:
1.记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算;
2.有计时功能,能显示用户开始答题后的消耗时间;
3.界面支持中文简体/中文繁体/英语,用户可以选择一种。
在软件开发中,我们需要大量使用工具来辅助设计,每个环节大家都要善于学习和使用工具。设计的时候,请使用思维导图设计你的模块之间的关系。

一、需求分析

1、用户输入数量可随机产生四则运算式
2、对用户输入的答案进行正确率分析
3、GUI界面:a、有计时功能,能显示用户开始答题后的消耗时间 b、界面支持中文简体/中文繁体/英语,用户可以选择一种;

二、思维导图

结对编程1——四则运算-GUI

三、关键代码说明

a计时功能

private void finishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishActionPerformed
// TODO add your handling code here:
rightanswer.setText("");
int correct = 0;
int fault = 0;
String[] anslist = enter2.getText().split("\n");
for (int i = 0; i < Answer.size(); i++) {
if (Answer.get(i).equals(anslist[i])) {
correct++;
rightanswer.append( "正确!答案是:"+Answer.get(i)+"\n");
} else {
rightanswer.append( " 错误!正确答案是:"+Answer.get(i)+"\n");
fault++;
}
}
String toDate = simpleFormat.format(new Date());
try {
to = simpleFormat.parse(toDate).getTime();
} catch (ParseException ex) {
Logger.getLogger(sizeyunsuan.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println((float) ((to - from) / (1000 * 60 * 60)));
if (language == 1) {
resultprintf.setText("答对" + correct + "题,正确率:" + (float) correct / (correct + fault) * 100 + "%,花费时间:" + (int) ((to - from) / (1000)) + "秒");
} else if (language == 2) {
resultprintf.setText("答對" + correct + "題,正確率:" + (float) correct / (correct + fault) * 100 + "%,花費時間:" + (int) ((to - from) / (1000)) + "秒");
} else if (language == 3) {
resultprintf.setText("Answer correct " + correct + "questions, correct rate:" + (float) correct / (correct + fault) * 100 + "%,Spend time:" + (int) ((to - from) / (1000)) + "s");
}
for (int i = 0; i < Answer.size(); i++) {
Answer.remove(i);
anslist = null;
}
}//GEN-LAST:event_finishActionPerformed

  b生成题目

private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
// TODO add your handling code here:
String num = enter1.getText();
int n = 0;
try {
n = Integer.parseInt(num);
} catch (NumberFormatException e) {
if (language == 1) {
resultprintf.setText("输入错误!请重新输入");
} else if (language == 2) {
resultprintf.setText("輸入錯誤!請重新輸入");
} else if (language == 3) {
resultprintf.setText("input error! please enter again");
}
}
int m = (int) (Math.random() * n);//随机整数题目和分数题目的题量
for (int i = 0; i < (n - m); i++) {//先随机出整数题型
Random random = new Random();
int n1 = random.nextInt(10);
int n2 = random.nextInt(10) + 1;
int a = (int) (Math.random() * 4 + 1);//随机决定运算类型
if (a == 1) {
Question.add(n1 + "+" + n2 + "=");
Answer.add(n1 + n2 + "");
}
if (a == 2) {
Question.add(n1 + "-" + n2 + "=");
Answer.add(n1 - n2 + "");
}
if (a == 3) {
Question.add(n1 + "×" + n2 + "=");
Answer.add(n1 * n2 + "");
}
if (a == 4) {
Question.add(n1 + "÷" + n2 + "=");
String n3 = (float) n1 / n2 + "";
if (n3.indexOf(".0") != -1) {
n3 = n3.replace(".0", "");
}
Answer.add((n3));
}
}
for (int i = 0; i < m; i++) {
int[] fn1 = createFraction();
int[] fn2 = createFraction();
int a = (int) (Math.random() * 4 + 1);
if (a == 1) {//加
Question.add("(" + Reduction(fn1[0], fn1[1]) + ")+(" + Reduction(fn2[0], fn2[1]) + ")=");
Answer.add(Reduction(((fn1[0] * fn2[1]) + (fn2[0] * fn1[1])), (fn1[1] * fn2[1])));//化简结果并存储
}
if (a == 2) {//减
Question.add("(" + Reduction(fn1[0], fn1[1]) + ")-(" + Reduction(fn2[0], fn2[1]) + ")=");
Answer.add(Reduction(((fn1[0] * fn2[1]) - (fn2[0] * fn1[1])), (fn1[1] * fn2[1])));
}
if (a == 3) {//乘
Question.add("(" + Reduction(fn1[0], fn1[1]) + ")×(" + Reduction(fn2[0], fn2[1]) + ")=");
Answer.add(Reduction(fn1[0] * fn2[0], fn1[1] * fn2[1]));//
}
if (a == 4) {//除
Question.add("(" + Reduction(fn1[0], fn1[1]) + ")÷(" + Reduction(fn2[0], fn2[1]) + ")=");
Answer.add(Reduction(fn1[0] * fn2[1], fn1[1] * fn2[0]));
}
}
int qn = 0;
question.setText("");
rightanswer.setText("");
enter2.setText("");
for (String string : Question) {
qn++;
question.append("[" + qn + "]" + string + "\n");
}
for (int i = 0; i < Question.size(); i++) {
Question.remove(i);
}
String fromDate = simpleFormat.format(new Date());
try {
from = simpleFormat.parse(fromDate).getTime();
} catch (ParseException ex) {
Logger.getLogger(sizeyunsuan.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_startActionPerformed

  c结果判断与输出

private void finishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishActionPerformed
// TODO add your handling code here:
rightanswer.setText("");
int correct = 0;
int fault = 0;
String[] anslist = enter2.getText().split("\n");
for (int i = 0; i < Answer.size(); i++) {
if (Answer.get(i).equals(anslist[i])) {
correct++;
rightanswer.append( "正确!答案是:"+Answer.get(i)+"\n");
} else {
rightanswer.append( " 错误!正确答案是:"+Answer.get(i)+"\n");
fault++;
}
}
String toDate = simpleFormat.format(new Date());
try {
to = simpleFormat.parse(toDate).getTime();
} catch (ParseException ex) {
Logger.getLogger(sizeyunsuan.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println((float) ((to - from) / (1000 * 60 * 60)));
if (language == 1) {
resultprintf.setText("答对" + correct + "题,正确率:" + (float) correct / (correct + fault) * 100 + "%,花费时间:" + (int) ((to - from) / (1000)) + "秒");
} else if (language == 2) {
resultprintf.setText("答對" + correct + "題,正確率:" + (float) correct / (correct + fault) * 100 + "%,花費時間:" + (int) ((to - from) / (1000)) + "秒");
} else if (language == 3) {
resultprintf.setText("Answer correct " + correct + "questions, correct rate:" + (float) correct / (correct + fault) * 100 + "%,Spend time:" + (int) ((to - from) / (1000)) + "s");
}
for (int i = 0; i < Answer.size(); i++) {
Answer.remove(i);
anslist = null;
}
}//GEN-LAST:event_finishActionPerformed

  d中文简体/中文繁体/英语三种语言的切换

private void language1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_language1ActionPerformed
// TODO add your handling code here:
language = 1;
title1.setText("四则运算生成器");
title2.setText("请输入产生四则算式的数量:");
title3.setText("请选择语言");
title4.setText("问题显示:");
title5.setText("输入答案:");
title6.setText("正确答案:");
start.setText("生成");
finish.setText("结束");
}//GEN-LAST:event_language1ActionPerformed

  

private void language2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_language2ActionPerformed
// TODO add your handling code here: language = 2;
title1.setText("四則運算生成器");
title2.setText("請輸入產生四則算式的數量:");
title3.setText("請選擇語言");
title4.setText("問題顯示:");
title5.setText("輸入答案:");
title6.setText("正確答案:");
start.setText("生成");
finish.setText("結束");
}//GEN-LAST:event_language2ActionPerformed

  

private void language3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_language3ActionPerformed
// TODO add your handling code here:
language = 3;
title1.setText("Arithmetic");
title2.setText("enter number:");
title3.setText("language");
title4.setText("problem display:");
title5.setText("Enter answer:");
title6.setText("proofreader:");
start.setText("enter");
finish.setText("end");
}//GEN-LAST:event_language3ActionPerformed

  

 e测试运行

简体中文:

结对编程1——四则运算-GUI

繁体中文

结对编程1——四则运算-GUI

英文

结对编程1——四则运算-GUI

结对照片展示:

结对编程1——四则运算-GUI

psp:

PSP2.1

Personal Software Process Stages

Estimated time

actual time

Planning

计划

15min

20min

· Estimate

估计这个任务需要多少时间

10h

12h

Development

开发

4h

3h

· Analysis

需求分析 (包括学习新技术)

5min

3min

· Design Spec

生成设计文档

10min

10min

· Design5 R20eview

设计复审

5min

5min

· Coding Standard

代码规范

30min

40min

· Design

具体设计

2h

2h

· Coding

具体编码

3h

4h

· Code Review

代码复审

20min

30min

· Test

测试(自我测试,修改代码,提交修改)

5min

10min

Reporting

报告

10min

10min

·

测试报告。

-

-

·

计算工作量

5min

5min

·

并提出过程改进计划

5min

6min

小结:

1、两人结对编程对于作业的纠错和细节处理是有明显帮助的。首先我们一起讨论了总体的需求以及该如何设计实现,然后一致决定采用黄建英同学的基础代码进行改进,实现四则运算的GUI界面。因为是基于她上次的代码,所以由黄建英同学负责代码的编写,而我则是负责提醒和审查。

2、虽然两个人的合作会使作业的时间变长,但是可以找出更多的不足之处,毕竟当局者迷。所以这一次通过合作,我们各自都有进步,也觉得这次的作业应该是比较精致的。

汉堡包式评价:

  1. 先来一片面包:我们两人达成结对编程的共识,采用JAVA和GUI实现
  2. 再把肉放上:这次编程过程中,我负责提醒和审查。但是期间遇到了诸多问题和不足,我们经过讨论、查资料就都顺利解决。
  3. 然后再来一片面包: 搭档的默契合作是一个好作品的完美助力。