• 第三次作业--(曹福亮,张义平)


    要求:

    在之前编写的四则运算程序基础之上做如下改进:

    1  请参照教材Page57:4.2-4.3节中内容,修改原程序,使之符合 “代码风格和设计规范”的基本要求;

    2  请采用模块化设计思想,修改之前的code,将 “计算功能” 封装起来

    3  通过测试程序和API 接口,测试其简单的加法功能。

    4  扩展程序功能(选做)

    针对第一次作业添加功能点支持:

    1). 程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.67而非2.66,或保持分数形式:8/3

    2). 可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。如: 2*(-4) = -8

    分析:

    1. 修改之前的代码,使代码符合代码风格和设计规范,使之更具有可观性;
    2. 对算法进行修改,使之可以计算负数;
    3. 对题目中的负数进行加括号处理,答案保留两位小数。

    设计:

    1. 在一些不易看明白的地方加注释使之更易看懂;
    2. 在java编程过程使用Format功能,使代码具有一定的代码风格;
    3. 对随机数进行加工,随机产生0~200之间的数进行减100处理;
    4. 除法中若出现除数为零的情况,则对除数进行加1处理;
    5. 为了使除法可以产生小数,把随机数设置为float型。

    代码实现:

      1 /**************************************************************
      2  *功能:四则运算程序设计 
      3  *编者:曹福亮、张义平
      4  *最后一次修改时间:2015年4月22日
      5  ***************************************************************/
      6 
      7 package View;
      8 
      9 import javax.swing.*;
     10 import java.util.*;
     11 import java.awt.*;
     12 import java.awt.event.*;
     13 
     14 public class Show extends JFrame implements ActionListener {
     15     // 出题部分
     16     JLabel jl1, jl2, jl3, jl4, jl6, jl8, jl9;
     17     JTextField jf1, jf2, jf3;
     18     JButton jb1, jb5;
     19     // 统计部分
     20     JButton jb;
     21     JTextField jl21, jl22, jl23, jl24, jl25, jl26, jl27, jl28, jl29, jl210,
     22             jl211, jl212, jl213, jl214, jl215;
     23     // 定义全局变量,包括随机数、答案、用户答案
     24     float i, j; // 随机数
     25     String a; // 答案
     26 
     27     public Show() {
     28         setTitle("计算加减乘除四则运算");
     29         setDefaultCloseOperation(EXIT_ON_CLOSE);
     30         setSize(874, 458);
     31         setLocation(200, 200);
     32         Container con = getContentPane();
     33         JPanel pan = new JPanel();
     34 
     35         // 出题、计算部分
     36         jb1 = new JButton("出题");
     37         jb1.addActionListener(this);
     38         jl1 = new JLabel("四则运算测试题库");
     39         jl1.setForeground(Color.BLUE);
     40         jl1.setFont(new Font("黑体", Font.BOLD, 30));
     41         jl2 = new JLabel("?");
     42         jl3 = new JLabel("答案:");
     43         jl4 = new JLabel("请输入答案:");
     44         // jl5 = new JLabel("余数:");
     45         jl6 = new JLabel("?");
     46         // jl7 = new JLabel("余数:");
     47         jl8 = new JLabel("对/错:");
     48         jl9 = new JLabel("?");
     49         jf1 = new JTextField(5);
     50         jf1.setEditable(false);
     51         jf2 = new JTextField(5);
     52         jf2.setEditable(false);
     53         jf3 = new JTextField(5);
     54         // jf4 = new JTextField(5);
     55         jb5 = new JButton("正确答案");
     56         jb5.addActionListener(this);
     57         Box b1 = Box.createHorizontalBox();
     58         b1.add(jb1);
     59         b1.add(Box.createHorizontalStrut(20));
     60         b1.add(jf1);
     61         b1.add(Box.createHorizontalStrut(5));
     62         b1.add(jl2);
     63         b1.add(Box.createHorizontalStrut(5));
     64         b1.add(jf2);
     65         b1.add(Box.createHorizontalStrut(5));
     66         b1.add(jl4);
     67         b1.add(Box.createHorizontalStrut(5));
     68         b1.add(jf3);
     69         // b1.add(Box.createHorizontalStrut(5));
     70         // b1.add(jl5);
     71         // b1.add(Box.createHorizontalStrut(5));
     72         // b1.add(jf4);
     73         b1.add(Box.createHorizontalStrut(20));
     74         b1.add(jb5);
     75         b1.add(Box.createHorizontalStrut(5));
     76         b1.add(jl3);
     77         b1.add(Box.createHorizontalStrut(5));
     78         b1.add(jl6);
     79         // b1.add(Box.createHorizontalStrut(5));
     80         // b1.add(jl7);
     81         // b1.add(Box.createHorizontalStrut(5));
     82         // b1.add(jl1);
     83         b1.add(Box.createHorizontalStrut(5));
     84         b1.add(jl8);
     85         b1.add(Box.createHorizontalStrut(5));
     86         b1.add(jl9);
     87 
     88         // 统计部分
     89         jl21 = new JTextField("0");
     90         jl22 = new JTextField("0");
     91         jl23 = new JTextField("0");
     92         jl24 = new JTextField("0");
     93         jl25 = new JTextField("0");
     94         jl26 = new JTextField("0");
     95         jl27 = new JTextField("0");
     96         jl28 = new JTextField("0");
     97         jl29 = new JTextField("0");
     98         jl210 = new JTextField("0");
     99         jl211 = new JTextField("0");
    100         jl212 = new JTextField("0");
    101         jl213 = new JTextField("0");
    102         jl214 = new JTextField("0");
    103         jl215 = new JTextField("0");
    104         jl21.setEditable(false);
    105         jl22.setEditable(false);
    106         jl23.setEditable(false);
    107         jl24.setEditable(false);
    108         jl25.setEditable(false);
    109         jl26.setEditable(false);
    110         jl27.setEditable(false);
    111         jl28.setEditable(false);
    112         jl29.setEditable(false);
    113         jl210.setEditable(false);
    114         jl211.setEditable(false);
    115         jl212.setEditable(false);
    116         jl213.setEditable(false);
    117         jl214.setEditable(false);
    118         jl215.setEditable(false);
    119 
    120         Box b21 = Box.createHorizontalBox();
    121         b21.add(new JLabel("加法题:"));
    122         b21.add(jl21);
    123         b21.add(Box.createHorizontalStrut(50));
    124         b21.add(new JLabel("对:"));
    125         b21.add(jl22);
    126         b21.add(Box.createHorizontalStrut(50));
    127         b21.add(new JLabel("错:"));
    128         b21.add(jl23);
    129 
    130         Box b22 = Box.createHorizontalBox();
    131         b22.add(new JLabel("减法题:"));
    132         b22.add(jl24);
    133         b22.add(Box.createHorizontalStrut(50));
    134         b22.add(new JLabel("对:"));
    135         b22.add(jl25);
    136         b22.add(Box.createHorizontalStrut(50));
    137         b22.add(new JLabel("错:"));
    138         b22.add(jl26);
    139 
    140         Box b23 = Box.createHorizontalBox();
    141         b23.add(new JLabel("乘法题:"));
    142         b23.add(jl27);
    143         b23.add(Box.createHorizontalStrut(50));
    144         b23.add(new JLabel("对:"));
    145         b23.add(jl28);
    146         b23.add(Box.createHorizontalStrut(50));
    147         b23.add(new JLabel("错:"));
    148         b23.add(jl29);
    149 
    150         Box b24 = Box.createHorizontalBox();
    151         b24.add(new JLabel("除法题:"));
    152         b24.add(jl210);
    153         b24.add(Box.createHorizontalStrut(50));
    154         b24.add(new JLabel("对:"));
    155         b24.add(jl211);
    156         b24.add(Box.createHorizontalStrut(50));
    157         b24.add(new JLabel("错:"));
    158         b24.add(jl212);
    159 
    160         Box b25 = Box.createHorizontalBox();
    161         b25.add(new JLabel("总 计:"));
    162         b25.add(jl213);
    163         b25.add(Box.createHorizontalStrut(50));
    164         b25.add(new JLabel("对:"));
    165         b25.add(jl214);
    166         b25.add(Box.createHorizontalStrut(50));
    167         b25.add(new JLabel("错:"));
    168         b25.add(jl215);
    169 
    170         Box b2 = Box.createVerticalBox();
    171         b2.add(b21);
    172         b2.add(b22);
    173         b2.add(b23);
    174         b2.add(b24);
    175         b2.add(b25);
    176 
    177         Box title = Box.createHorizontalBox();
    178         title.add(jl1);
    179 
    180         Box b3 = Box.createVerticalBox();
    181         b3.add(Box.createVerticalStrut(30));
    182         b3.add(title);
    183         b3.add(Box.createVerticalStrut(30));
    184         b3.add(b1);
    185         b3.add(Box.createVerticalStrut(25));
    186         b3.add(b2);
    187         b3.add(Box.createVerticalStrut(25));
    188         jb = new JButton("清除记录");
    189         jb.addActionListener(this);
    190         b3.add(jb);
    191         b3.add(Box.createVerticalStrut(50));
    192         Box zs = Box.createVerticalBox();
    193         Box zs1 = Box.createHorizontalBox();
    194         zs1.add(new JLabel("注:本题库出题均带有一位小数的整数,所以在输入答案时也需要输入一位小数"));
    195         Box zs2 = Box.createHorizontalBox();
    196         zs2.add(new JLabel("才行,如果算法是除法, 则需要进行四舍五入并保留两位有效数字。"));
    197         zs.add(zs1);
    198         zs.add(zs2);
    199         b3.add(zs);
    200         pan.add(b3);
    201         con.add(pan);
    202         setVisible(true);
    203         // pack();
    204     }
    205 
    206     // 调用主函数,显示用户界面
    207     public static void main(String args[]) {
    208         Show main = new Show();
    
    209     }
    210 
    211     // 按钮监听器的动作处理
    212     public void actionPerformed(ActionEvent e) {
    213         // 清楚记录按钮
    214         if (e.getSource() == jb) {
    215             jf1.setText("");
    216             jf2.setText("");
    217             jf3.setText("");
    218             // jf4.setText("");
    219             // jl1.setText("?");
    220             jl2.setText("?");
    221             jl6.setText("?");
    222             jl9.setText("?");
    223             jl21.setText("0");
    224             jl22.setText("0");
    225             jl23.setText("0");
    226             jl24.setText("0");
    227             jl25.setText("0");
    228             jl26.setText("0");
    229             jl27.setText("0");
    230             jl28.setText("0");
    231             jl29.setText("0");
    232             jl210.setText("0");
    233             jl211.setText("0");
    234             jl212.setText("0");
    235             jl213.setText("0");
    236             jl214.setText("0");
    237             jl215.setText("0");
    238         }
    239 
    240         // 出题按钮
    241         if (e.getSource() == jb1) {
    242             jf3.setText("");
    243             // jf4.setText("");
    244             // jl1.setText("?");
    245             jl2.setText("?");
    246             jl6.setText("?");
    247             jl9.setText("?");
    248 
    249             // 产生两个随机数
    250             Random rand = new Random();
    251             i = rand.nextInt(200);
    252             j = rand.nextInt(200);
    253             i = i - 100;
    254             j = j - 100;
    255             if (i < 0) {
    256                 String s = Float.toString(i);
    257                 jf1.setText("(" + s + ")");
    258 
    259             } else {
    260                 String s = Float.toString(i);
    261                 jf1.setText(s);
    262             }
    263             if (j < 0) {
    264                 String y = Float.toString(j);
    265                 jf2.setText("(" + y + ")");
    266             } else {
    267                 String y = Float.toString(j);
    268                 jf2.setText(y);
    269             }
    270 
    271             // 产生随机算法
    272             int f = rand.nextInt();
    273             f = rand.nextInt(4);
    274             if (f == 0) {
    275                 jl2.setText("+");
    276             } else if (f == 1) {
    277                 jl2.setText("-");
    278             } else if (f == 2) {
    279                 jl2.setText("*");
    280             } else if (f == 3) {
    281                 jl2.setText("/");
    282             }
    283         }
    284 
    285         // 判断对错且给出正确答案按钮
    286         if (e.getSource() == jb5) {
    287             int b, c, d, f, h, g, k, l, m, n, o, p;
    288             int r, t, u;
    289             r = Integer.parseInt(jl213.getText()) + 1;
    290             jl213.setText(Integer.toString(r));
    291             // jl1.setText("?");
    292             double answer = 0;
    293             String z, x, s = null;
    294             z = jl2.getText();
    295             x = jf3.getText();
    296             // j = jf4.getText();
    297 
    298             // 四则运算部分
    299             if (z == "+") {
    300                 b = Integer.parseInt(jl21.getText()) + 1;
    301                 jl21.setText(Integer.toString(b));
    302                 answer = i + j;
    303                 a = Double.toString(answer);
    304             } else if (z == "-") {
    305                 f = Integer.parseInt(jl24.getText()) + 1;
    306                 jl24.setText(Integer.toString(f));
    307                 answer = i - j;
    308                 a = Double.toString(answer);
    309             } else if (z == "*") {
    310                 k = Integer.parseInt(jl27.getText()) + 1;
    311                 jl27.setText(Integer.toString(k));
    312                 answer = i * j;
    313                 a = Double.toString(answer);
    314             } else if (z == "/") {
    315                 if (j == 0) {
    316                     j = j + 1;
    317                     n = Integer.parseInt(jl210.getText()) + 1;
    318                     jl210.setText(Integer.toString(n));
    319                     answer = i / j;
    320                     // a = Double.toString(answer);
    321                     a = String.format("%.2f", answer);
    322                     // jl1.setText("0");
    323                 } else {
    324                     n = Integer.parseInt(jl210.getText()) + 1;
    325                     jl210.setText(Integer.toString(n));
    326                     answer = i / j;
    327                     a = String.format("%.2f", answer);
    328                 }
    329 
    330             }
    331             jl6.setText(a);
    332 
    333             // 系统判断对错部分
    334             if (x.equals(a)) {
    335                 if (s == null) {
    336                     jl9.setText("对");
    337                     t = Integer.parseInt(jl214.getText()) + 1;
    338                     jl214.setText(Integer.toString(t));
    339                     if (z == "+") {
    340                         c = Integer.parseInt(jl22.getText()) + 1;
    341                         jl22.setText(Integer.toString(c));
    342                     } else if (z == "-") {
    343                         h = Integer.parseInt(jl25.getText()) + 1;
    344                         jl25.setText(Integer.toString(h));
    345                     } else if (z == "*") {
    346                         l = Integer.parseInt(jl28.getText()) + 1;
    347                         jl28.setText(Integer.toString(l));
    348                     } else if (z == "/") {
    349                         o = Integer.parseInt(jl211.getText()) + 1;
    350                         jl211.setText(Integer.toString(o));
    351                     }
    352                 }// else if (j.equals(s)) {
    353                 // jl9.setText("对");
    354                 // t = Integer.parseInt(jl214.getText()) + 1;
    355                 // jl214.setText(Integer.toString(t));
    356                 // if (z == "+") {
    357                 // c = Integer.parseInt(jl22.getText()) + 1;
    358                 // jl22.setText(Integer.toString(c));
    359                 // } else if (z == "-") {
    360                 // h = Integer.parseInt(jl25.getText()) + 1;
    361                 // jl25.setText(Integer.toString(h));
    362                 // } else if (z == "*") {
    363                 // l = Integer.parseInt(jl28.getText()) + 1;
    364                 // jl28.setText(Integer.toString(l));
    365                 // } else if (z == "/") {
    366                 // o = Integer.parseInt(jl211.getText()) + 1;
    367                 // jl211.setText(Integer.toString(o));
    368                 // }
    369                 // }
    370                 else {
    371                     jl9.setText("错");
    372                     u = Integer.parseInt(jl215.getText()) + 1;
    373                     jl215.setText(Integer.toString(u));
    374                     if (z == "+") {
    375                         d = Integer.parseInt(jl23.getText()) + 1;
    376                         jl23.setText(Integer.toString(d));
    377                     } else if (z == "-") {
    378                         g = Integer.parseInt(jl26.getText()) + 1;
    379                         jl26.setText(Integer.toString(g));
    380                     } else if (z == "*") {
    381                         m = Integer.parseInt(jl29.getText()) + 1;
    382                         jl29.setText(Integer.toString(m));
    383                     } else if (z == "/") {
    384                         p = Integer.parseInt(jl212.getText()) + 1;
    385                         jl212.setText(Integer.toString(p));
    386                     }
    387                 }
    388             } else {
    389                 jl9.setText("错");
    390                 u = Integer.parseInt(jl215.getText()) + 1;
    391                 jl215.setText(Integer.toString(u));
    392                 if (z == "+") {
    393                     d = Integer.parseInt(jl23.getText()) + 1;
    394                     jl23.setText(Integer.toString(d));
    395                 } else if (z == "-") {
    396                     g = Integer.parseInt(jl26.getText()) + 1;
    397                     jl26.setText(Integer.toString(g));
    398                 } else if (z == "*") {
    399                     m = Integer.parseInt(jl29.getText()) + 1;
    400                     jl29.setText(Integer.toString(m));
    401                 } else if (z == "/") {
    402                     p = Integer.parseInt(jl212.getText()) + 1;
    403                     jl212.setText(Integer.toString(p));
    404                 }
    405             }
    406         }
    407     }
    408 }

    运行结果:

    两人合作步骤:

    1. 首先各自都题目要求进行分析,分享各自对题目的理解;
    2. 考虑到时间有限的情况下,共同得出不进行重写代码的结论;
    3. 由于我之前的代码比较独特,所以选择修改我之前的程序来完成本次作业;
    4. 在修改的过程中共同讨论需要修改和保留的地方;
    5. 进行分工合作的方式,不懂的需要上网查资料的地方,一起探讨解决方法;
    6. 最后进行代码测试,对运行结果进行测试,检测四则运算的算法是否正确。

     PSP耗时:

    步骤 耗时 百分比(%)
    题目分析 10 6
    讨论 20 12
    具体设计 25 15
    具体代码 50 30
    代码复审 20 12
    测试 24 13
    总结 20 12

     总结:

    不懂的地方太多,结果还是任性的进行了自我选择性的做题;

    看到题目一次次的加大难度,我感觉有点虚了,作业来得太快,我有点承受不来!!!

    作者:曹福亮         时间:2015/4/22

  • 相关阅读:
    11-14序列化模块之json、pickle、shelve
    11-13 模块_collections(不太重要)&time&random&os
    Python常用标准库之datetime、random、hashlib、itertools
    模块安装说明
    __name__=='__main__'作用
    10-29 继承-单继承
    10-12 面向对象初级
    栈内存 堆内存
    【初识MyBatis→简单的mybatis开发环境搭建】
    【Linux常用命令小手册】
  • 原文地址:https://www.cnblogs.com/wjfsrc/p/4448814.html
Copyright © 2020-2023  润新知