测试与封装 5.2

程序开发简介:

【开发环境】:eclipse

【开发人员】:Ives & 郑胜斌

【博客地址】:38郑胜斌

【开发时间】:2015-05-08

【版本】:5.2

【协同工作】:

      这次主要是添加减法乘法除法,保证除数不为0,修复点击“再来”会重新出题

【未解决问题】:

      判断文本框为空没有反应

      if(textField_3.getText()=="")

      要把 == 改成 != 才有反应

界面代码如下:

  1 package com.ives;
  2
  3 import java.awt.EventQueue;
  4
  5 public class frame {
  6
  7     private JFrame frame;
  8     private JTextField textField;
  9     private JTextField textField_1;
 10     private JTextField textField_2;
 11     private JTextField textField_3;
 12     private JTextField textField_4;
 13     private JTextField textField_5;
 14     frame f;
 15
 16     int a;
 17     int b;
 18     char []op={'+','-','*','/'};
 19     String Sa;
 20     String Sb;
 21     int result;
 22     static int i = (int) (Math.random() * 3);
 23
 24
 25     Expression expression = new Expression();
 26     private JButton btnNewButton_1;
 27
 28
 29     /**
 30      * Launch the application.
 31      */
 32     public static void main(String[] args) {
 33         EventQueue.invokeLater(new Runnable() {
 34             public void run() {
 35                 try {
 36                     frame window = new frame();
 37                     window.frame.setVisible(true);System.out.print(i);
 38                 } catch (Exception e) {
 39                     e.printStackTrace();
 40                 }
 41             }
 42         });
 43     }
 44
 45     /**
 46      * Create the application.
 47      * @throws Yichang
 48      */
 49     public frame() throws Yichang {
 50         initialize();
 51     }
 52
 53     /**
 54      * Initialize the contents of the frame.
 55      */
 56     private void initialize() throws Yichang{
 57         frame = new JFrame();
 58         frame.setBounds(100, 100, 517, 352);
 59         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 60
 61         JPanel panel = new JPanel();
 62         frame.getContentPane().add(panel, BorderLayout.CENTER);
 63         panel.setLayout(null);
 64
 65         JLabel lblNewLabel = new JLabel("\u56DB\u5219\u8FD0\u7B97\u56685.2");
 66         lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 67         lblNewLabel.setBounds(141, 0, 208, 57);
 68         panel.add(lblNewLabel);
 69
 70         textField = new JTextField();
 71         textField.setHorizontalAlignment(SwingConstants.CENTER);
 72         textField.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 73         textField.setBounds(56, 82, 88, 45);
 74         panel.add(textField);
 75         textField.setColumns(10);
 76
 77         textField_1 = new JTextField();
 78         textField_1.setHorizontalAlignment(SwingConstants.CENTER);
 79         textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 80         textField_1.setColumns(10);
 81         textField_1.setBounds(220, 82, 88, 45);
 82         panel.add(textField_1);
 83
 84         textField_2 = new JTextField("=");
 85         textField_2.setHorizontalAlignment(SwingConstants.CENTER);
 86         textField_2.setEditable(false);
 87         textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 88         textField_2.setColumns(10);
 89         textField_2.setBounds(318, 82, 36, 45);
 90         panel.add(textField_2);
 91
 92         textField_3 = new JTextField();
 93         textField_3.setHorizontalAlignment(SwingConstants.CENTER);
 94         textField_3.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 95         textField_3.setColumns(10);
 96         textField_3.setBounds(371, 82, 88, 45);
 97         panel.add(textField_3);
 98
 99         JButton btnNewButton = new JButton("\u505A\u5B8C\u4E86");
100         btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 30));
101         btnNewButton.setBounds(188, 156, 131, 45);
102         panel.add(btnNewButton);
103
104         JLabel lblNewLabel_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");
105         lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
106         lblNewLabel_1.setBounds(56, 222, 131, 35);
107         panel.add(lblNewLabel_1);
108
109         textField_4 = new JTextField();
110         textField_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));
111         textField_4.setColumns(10);
112         textField_4.setBounds(188, 222, 161, 35);
113         panel.add(textField_4);
114
115         textField_5 = new JTextField();
116         textField_5.setHorizontalAlignment(SwingConstants.CENTER);
117         textField_5.setEditable(false);
118         textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 30));
119         textField_5.setText(String.valueOf(op[i]));
120         textField_5.setColumns(10);
121         textField_5.setBounds(159, 82, 44, 45);
122         panel.add(textField_5);
123
124         a = expression.geta();
125         Sa = String.valueOf(a);
126         textField.setText(Sa);
127
128         b = expression.getb();
129         Sb = String.valueOf(b);
130         textField_1.setText(Sb);
131
132         btnNewButton_1 = new JButton("\u518D\u6765 ");
133         btnNewButton_1.addActionListener(new ActionListener() {
134             public void actionPerformed(ActionEvent e) {
135
136                 try {
137                         f = new frame();
138                 } catch (Yichang e1) {
139                     // TODO 自动生成的 catch 块
140                     e1.printStackTrace();
141                 }
142                 frame.dispose();
143                 f.frame.setVisible(true);
144
145             }
146         });
147         btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
148         btnNewButton_1.setBounds(366, 222, 107, 36);
149         panel.add(btnNewButton_1);
150
151
152         btnNewButton.addActionListener(new ActionListener() {
153             public void actionPerformed(ActionEvent arg0){
154                 int n3=Integer.valueOf(textField_3.getText().toString());
155                 switch(op[i])
156                 {
157                     case '+':result = expression.expressionadd();break;
158                     case '-':result = expression.expressionmin();break;
159                     case '*':result = expression.expressionchen();break;
160                     case '/':result = expression.expressionchu();break;
161                 }
162                 if(textField_3.getText()!="")
163                 {
164                      if(result==n3)
165                         {
166                             textField_4.setText(" 正确");
167                         }
168                          else
169                         {
170                             textField_4.setText(" 错误!答案为"+result);
171                         }
172                 }
173                 try {
174                     if(textField_3.getText()=="")
175                     throw new Yichang("不能为空!");
176                     } catch (Yichang e) {
177                         // TODO 自动生成的 catch 块
178                         e.printStackTrace();
179                     }
180
181                 /*try {
182                  if(n3<0)
183
184                         throw new Yichang("不可能是负数!");
185                     } catch (Yichang e) {
186                         // TODO 自动生成的 catch 块
187                         e.printStackTrace();
188                     }*/
189
190                 }
191         });
192     }
193 }

Frame

计算逻辑代码如下:

 1 package com.ives;
 2 import java.util.*;
 3
 4 import com.ives.Input;
 5 public class Expression
 6 {
 7     private int a = (int)(Math.random() * Math.pow(10,2)-1);
 8     private int b = (int)(Math.random() * Math.pow(10,2)-1);
 9     private int op;
10     static int c;//用户答案
11     int answer;//答案
12     int answer1;
13     static Scanner in=new Scanner(System.in);
14
15 public int geta()
16 {
17     return a;
18 }
19
20 public void seta(int a)
21 {
22     this.a = a;
23 }
24
25 public int getb()
26 {
27     return b;
28 }
29
30 public void setb(int b)
31 {
32     this.b = b;
33 }
34
35 public int expressionadd()
36 {
37     answer = a+b;
38     return answer;
39 }
40 public int expressionmin()
41 {
42     answer = a-b;
43     return answer;
44 }
45 public int expressionchen()
46 {
47     answer = a*b;
48     return answer;
49 }
50 public int expressionchu()
51 {
52     b = (b == 0 ? b = 1 +(int)(Math.random() * Math.pow(10,2)-1):b);//排除除法被除数为0的情况
53     answer = a/b;
54     return answer;
55 }
56 public static void main(String[] args){
57     int answer;
58     Expression expression = new Expression();
59
60     //answer = expression.answer;
61     //Input input = new Input();
62     //Expression.c = input.a;
63     /*try{
64         Expression.c = in.nextInt();
65     }
66     catch(InputMismatchException e)
67     {   System.err.println("\n错误!  ,请你输入一个整数");
68     }*/
69     //if(answer==c)
70 /*    {
71         System.out.print("答对了");
72     }
73     else System.out.print("答错了");*/
74     //System.out.print("answer="+answer);
75 }
76 }

Expression

输入代码如下:

 1 package com.ives;
 2 import java.util.*;
 3
 4 public class Input {
 5     public int a;
 6     static Scanner in=new Scanner(System.in);
 7     public void seta(int intput){
 8         a = intput;
 9     }
10     public Input()
11     {
12         try{
13             a = in.nextInt();
14         }
15         catch(InputMismatchException e)
16         {
17             System.err.println("\n错误!  请你输入一个整数!\n");
18         }
19     }
20     public int getinput()
21     {
22         return a;
23     }
24
25 }

Input

运行截图如下:

这里点击再来会重新出题.

这次的单元测试和5.1的一样的。

个人体会:

这次做这个5.2的实在有点。。。因为小伙伴的组织的工作实在有点忙,不过最终还是完成了,不是有句话说,有志者事竟成咩。时间挤一挤总会有的。这奥次5.2的作业老师是有提到用测试驱动开发做的。什么是测试驱动开发呢?这是一种开发模型,就是在编写代码前先写测试的代码,然后在编写核心代码。开始的时候我以为是测试驱动,开发。其实原来是测试-驱动-开发。就是测试促进开发。对于这种敏捷开发的极限编程开发模型,我会在以后的开发里面尝试使用下这种开发模型的。

             我会尝试下把这个计算功能做成一个app的,大家期待吧。~

-----------------此次的作业报告到此结束,谢谢老师审阅-----------------------

转载于:https://www.cnblogs.com/IvesHe/p/4488633.html

【作业报告】作业5 四则运算 测试与封装 5.2(已更新)相关推荐

  1. c语言四则运算程序报告,C语言四则运算测试程序设计报告

    <C语言四则运算测试程序设计报告>由会员分享,可在线阅读,更多相关<C语言四则运算测试程序设计报告(23页珍藏版)>请在人人文库网上搜索. 1.昆明理工大学计算机程序设计基础课 ...

  2. 5.1作业5 四则运算 测试与封装

    这次作业是我们小组成员我(21林庆光)和同伴(13卢盛添)的结伴作业,按照老师的要求我们换了同伴组队来完成这次的作业.这次的作业是在上次作业四则运算的基础上完成的,实现了对其计算功能类的封装,完成代码 ...

  3. 练习5.1更新——四则运算 测试与封装

    package com.h1;import static org.junit.Assert.*;import org.junit.Assert; import org.junit.Before; im ...

  4. c语言作业报告,C语言程序设计综合作业报告——作业管理系统

    1.1系统概要 (1) 进入系统之前,用户输入密码1234,进入,共有3次机会. (2) 用户根据需要输入(0-5)或(0-6)实现不同的功能,若输入其他字符,则显示按键错误,并返回界面让用户重新选择 ...

  5. 【编译原理】东北大学2022春季大作业之作业一【40%】编译一个TEX项目【已更新】【命令行编译】【texworks编译】【回答了一些问题】

    目录 前言 作业一[编译一个TEX项目] 1.开源网站 2.任务 3.作业完成流程 [修改一--miktex更新时选择管理员身份] [这里针对于评论区小伙伴的提问进行解答] [有关于622页的解答] ...

  6. sql server作业_在SQL Server中报告作业失败并发出警报

    sql server作业 SQL Server Agent can be used to run a wide variety of tasks within SQL Server. The buil ...

  7. 《spark技术应用》课程期末考试大作业报告,使用eclipse完成求top值、文件排序、二次排序三个程序的个性化开发。

    ​​​​目录 一.选题的目的及要求... 4 二.设计思路... 4 三.主要内容及关键技术.. 5 四.制作步骤... 5 1.准备工作... 5 1.1在VMware中安装一台Ubuntu64位系 ...

  8. 计算机地质应用软件,中国地质大学《质软件应用》作业报告.doc

    中国地质大学<质软件应用>作业报告 计算机地质应用软件 MapGIS软件实验报告 学号: 姓名: 实验目的 实验任务 实验步骤 建立新的MapGIS工程: 点击主菜单"图形处理& ...

  9. 计算机硬件大型作业报告,计算机硬件技术大作业报告.doc

    计算机硬件技术大作业报告 计算机硬件技术实践报告 题目 温度测量系统设计 姓名 专业 测控技术与仪器 班级 学号 上海电力学院自动化工程学院 目录: (1) 设计题目 () 开发目的 (3) 小组成员 ...

  10. 计算机软件技术大作业报告,多媒体技术大作业报告.doc

    多媒体技术大作业报告.doc 华南理工大学 多媒体技术 大作业报告 专 业: 班 级: 学 号: 学生姓名: 完成时间: 目录 说 明 --------------------3 第一章概述 ---- ...

最新文章

  1. VS2017+mysql5.7 连接数据库生成实体
  2. Android移动开发之【Android实战项目】在Service中弹出Dialog对话框,即全局性对话框
  3. sourcetree打开快捷_Sourcetree使用:拉取打开项目的步骤
  4. 概率论与数理统计-ch8-假设检验
  5. 安装过程中又一个问题出现了。
  6. nth-of-type和nth-child的区别
  7. 达人评测 i510400f和i511400f区别 i5 11400f和i5 10400f对比
  8. 2019 版idea设置字体大小
  9. cannot set options after executing query
  10. windows linux 共存,Windows与Linux共存
  11. Oracle 12c 数据库可插拔体系结构
  12. 那些好看的渐变色linear-gradient(拿走不谢)
  13. 1GB有多少个扇区?
  14. Jim Zemlin:中国是开源最重要的市场
  15. vue + html2canvas + ArcGIS 3.x 地图一键截图功能踩坑之路(二)
  16. 外汇术语和缩略语解释
  17. GitHub牛逼开源项目!像写 Markdown 一样画流程图
  18. ubuntu22.04图文安装流程
  19. 添加VBA控件按钮及操作提示框
  20. 十年总结(24):现在的我

热门文章

  1. 【最短路】 ZOJ 1544 Currency Exchange 判断负圈
  2. 2013年全国各大著名的IT公司薪资待遇大揭密 给出入职场的民工一点建议
  3. C#中的变量类型var
  4. ORACLE 常用函数——字符函数
  5. RHEL5.X 重启网卡出现./network-functions: line 78: .: ifc
  6. [译]C语言实现一个简易的Hash table(2)
  7. Spring Security——基于表单登录认证原理及实现
  8. webpack环境的配置
  9. 分布式监控系统Zabbix3.2给异常添加邮件报警
  10. 无人自助便利店采用射频识别技术 30秒钟就能完成付款