【作业报告】作业5 四则运算 测试与封装 5.2(已更新)
测试与封装 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 }
计算逻辑代码如下:
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 }
输入代码如下:
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 }
运行截图如下:
这里点击再来会重新出题.
这次的单元测试和5.1的一样的。
个人体会:
这次做这个5.2的实在有点。。。因为小伙伴的组织的工作实在有点忙,不过最终还是完成了,不是有句话说,有志者事竟成咩。时间挤一挤总会有的。这奥次5.2的作业老师是有提到用测试驱动开发做的。什么是测试驱动开发呢?这是一种开发模型,就是在编写代码前先写测试的代码,然后在编写核心代码。开始的时候我以为是测试驱动,开发。其实原来是测试-驱动-开发。就是测试促进开发。对于这种敏捷开发的极限编程开发模型,我会在以后的开发里面尝试使用下这种开发模型的。
我会尝试下把这个计算功能做成一个app的,大家期待吧。~
-----------------此次的作业报告到此结束,谢谢老师审阅-----------------------