【作業報告】作業5 四則運算 測試與封裝 5.2(已更新)

蘇亦晨發表於2015-05-08

測試與封裝 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的,大家期待吧。~

-----------------此次的作業報告到此結束,謝謝老師審閱-----------------------

相關文章