- GitHub源码地址:https://github.com/nigulasiletai/Demo2
- 1.需求分析:
- (1)随机选择20道混合运算;
-
(2)用户输入答案,给出结果;
(3)程序检查答案结果,正确计5分,错误不计分;
(4)20道测试题结束给出总分;
(5)带括号算式,真分数算式;
(6)程序允许用户进行多轮测试,给出多轮测试分数柱状图;
(7)程序记录用户答题结果,当程序退出再启动的时候,可为用户显示最后一次结果,并询问用户可否进行新一轮测试;
(8)测试有记时功能,动态显示答题后的消耗时间;
(9)程序人机交互界面是GUI界面;
2.功能设计:
(1)算式引用实验二的代码;
(2)生成的题库显示在QuestionBank.docx 中;并随机产生20道算式;
(3)调用包JfreeChart-1.0.13包;来生成柱状图;
(4)倒计时用Thread.sleep和循环来完成;
(5)多轮测试弹出框用JOptionPane类来完成;
3.设计实现:
(1)随机产生的20道算式放在ArrayList集合中,然后传参调入Gu类中;
(2)倒计时时间设置为30分钟,以消耗的形式动态展现;
(3)随机产生的算式得出的结果放在counter中,然后以传参形式放在Score类中,形成柱状图;
4.核心代码:
Gu类(界面)
package cn.longteng.demo; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.FileNotFoundException; import java.util.ArrayList; import javax.swing.*; public class Gu extends JFrame { static JFrame j; static JPanel jp, p1, p2; JLabel[] lab = new JLabel[20]; JLabel[] lab3 = new JLabel[20]; static JTextField[] txt = new JTextField[20]; static JButton btn; static JLabel lab1; String[] str = new String[200]; int[] num = new int[200]; String[] sr = new String[20]; static int[] nm = new int[20]; static long time = 1800; static Main m = new Main(); static int[] res=new int[20]; static int counter=0; static String[] srs=new String[20]; static int n; static int w; static ArrayList<Integer> lis=new ArrayList(); public Gu() { } public void Jiemian(){ j = new JFrame("小学生在线混合运算题库"); j.setSize(1366, 735); jp = new JPanel(); jp.setLayout(new BorderLayout()); p1 = new JPanel(); p1.setLayout(new BorderLayout()); p2 = new JPanel(); p2.setLayout(new GridLayout(10, 5, 10, 40)); btn = new JButton("提交"); lab1 = new JLabel(); for (int k = 0; k < 20; k++) { lab[k] = new JLabel(""); txt[k] = new JTextField(" "); lab3[k] = new JLabel(" "); } for (int i = 0; i < 20; i++) { p2.add(lab3[i]); p2.add(lab[i]); p2.add(txt[i]); } p1.add(lab1, BorderLayout.WEST); p1.add(btn, BorderLayout.EAST); j.add(jp); jp.add(p1, BorderLayout.NORTH); jp.add(p2, BorderLayout.CENTER); j.setVisible(true); j.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); } public void SetLab(Main s) { try { s.Num(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.str = Main.ts; this.num = Main.s; int i = 0; for (int l = 0; l < 200; l = l + 10) { while (i < 20) { sr[i] = str[l]; nm[i] = num[l]; lab[i].setText(sr[i]); i++; break; } } } public void Time() { while(time >0) { long mm = time / 60 % 60; long ss = time % 60; lab1.setText("剩余答题时间:" + mm + ":" + ss); time--; try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void Chann(){ btn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==btn){ for(int x=0;x<20;x++){ srs[x]=txt[x].getText().replaceAll(" ",""); if(srs[x].equals("")){ res[x]=0; } else{ res[x]=Integer.parseInt(srs[x].replaceAll(" ", "")); if(res[x]==nm[x]){ counter+=5; } } } lis.add(counter); n=JOptionPane.showConfirmDialog(j, "是否提交?","系统提示",JOptionPane.INFORMATION_MESSAGE); if(n==JOptionPane.YES_OPTION){ j.dispose(); w++; new Score(); } else{ return; } } } }); } public void Ch(){ if(time==0){ for(int k=0;k<20;k++){ srs[k]=txt[k].getText().replaceAll(" ",""); if(srs[k].equals("")){ res[k]=0; }else{ res[k]=Integer.parseInt(srs[k].replaceAll(" ", "")); if(res[k]==nm[k]){ counter+=5; } } } lis.add(counter); j.dispose(); w++; new Score(); } } }
Score类
package cn.longteng.demo; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JOptionPane; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; public class Score extends JFrame{ ChartPanel frame1; static JFrame frame; static Gu gu=new Gu(); static Main m = new Main(); static ArrayList<Integer> lis=new ArrayList(); public Score(){ CategoryDataset dataset=getData(gu); JFreeChart chart=ChartFactory.createBarChart3D("小学生在线测试分数结果", "测试的次数", "分数", dataset, PlotOrientation.VERTICAL, true, false,false); CategoryPlot plot=chart.getCategoryPlot(); CategoryAxis domainAxis=plot.getDomainAxis(); domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14)); //水平底部标题 domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //垂直标题 domainAxis.setUpperMargin(0.2); //设置距离图片右端距离 domainAxis.setLowerMargin(0.1); //透明度 // plot.setForegroundAlpha(1.0f); ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状 rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15)); chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); chart.getTitle().setFont(new Font("宋体",Font.BOLD,20));//设置标题字体 BarRenderer3D renderer = new BarRenderer3D();//3D属性修改 plot.setRenderer(renderer);//将修改后的属性值保存到图中 renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelPaint(Color.BLUE);// frame1=new ChartPanel(chart,true); frame=new JFrame("小学生在线混合运算测试统计图"); frame.setLayout(new GridLayout(2,2,10,10)); frame.add(getChartPanel()); //添加柱形图 frame.setBounds(300, 50, 600, 650); frame.setVisible(true); Clos(); } public CategoryDataset getData(Gu g) { // TODO Auto-generated method stub DefaultCategoryDataset dataset = new DefaultCategoryDataset(); this.lis=g.lis; for(int i=0;i<g.w;i++){ dataset.addValue(lis.get(i),"","第"+i+"轮测试"); } return dataset; } public ChartPanel getChartPanel(){ return frame1; } public static void Clos(){ frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ int i=JOptionPane.showConfirmDialog(null, "是否再次测试","系统提示",JOptionPane.INFORMATION_MESSAGE); if(i==JOptionPane.YES_OPTION){ Gu g = new Gu(); g.Jiemian(); g.SetLab(m); g.Chann(); frame.dispose(); //g.Time(); g.Ch(); } else { System.exit(0); } } }); } }
5.测试结果截图:
结对时:
6.PSP:
PSP2.1 任务内容 计划完成需要的时间(min) 实际完成的时间(min) planning 计划 30 40 Estimate 估计这个任务需要多少时间 40 55 Development 开发 100 120 Analysis 需求分析 8 20 Design Spec 生成设计文档 8 19 Design Review 设计审查 5 5 Coding Standard 代码规范 14 9 Design 具体设计 20 31 Coding 具体编码 60 110 Code Review 代码复审 38 15 Test 测试(自我测试,修改代码,提交修改) 8 20 Reporting 报告 16 14 Test Report 测试报告 10 13 Size Measurement 计算工作量 4 6 Postmortem & Process
Improvement
事后总结 10 12 - 7.总结
- 通过本次的结对项目明白了团结合作的重要性,同时也清楚了自身的不足 ,在这次的结对项目实验中我的小伙伴马中山充分展现了他的能力,每当遇到难题时,总是他在第一时间反应过来,找出正确的方向,并且解决问题,这一点我应该向他学习。而我的问题是对代码的理解与阅读不够,需要在课后多加练习,希望在今后的项目中能展显出更好的成绩。