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

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

 

 

开发环境:   Eclipse

开发人员:   欧其锋(201306114305)、余汉城(201306114317)

所用时间:   大约3个小时

博客地址:   http://www.cnblogs.com/ouqifeng/

 

 


 

 

1.第一阶段目标 - 把计算的功能封装成类。

 1 package GongNengpk;
 2 
 3 public class HeXinCode {
 4 
 5     int temp=0;
 6     int answer=0;
 7     public int JJCC(char op,int a,int b)
 8     {
 9         switch(op) 
10         {  
11         case '+':   answer=a+b;   break;
12         case '-':   answer=a-b;   break;
13         case '*':   answer=a*b;   break;
14         case '/':   answer=a/b;   break;  
15         } 
16         return answer;
17     }
18     public int JC(int c)
19     {
20         int jc=1;
21         for(int i=c;i>0;i--)
22         {
23             jc=jc*i;
24         }
25         answer=jc;
26         return answer;
27     }
28     
29 }
HeXinCode

        简单的把加减乘除和阶乘算法封装成一个Class。

 

2.设计测试用例:用白盒与黑盒测试设计技术,为计算核心设计测试用例。

   白盒测试:

         

 

 

 

  黑盒测试:

 

 

3.在实验环境中(如MyEclipse集成开发环境+Junit测试框架)运行测试用例,分析测试结果,找出程序问题,给出改进前后的代码对照。

Junit测试

测试源代码:

 1 package GongNengpkTest;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Test;
 6 
 7 import GongNengpk.HeXinCode;
 8 
 9 public class HeXinCodeTest {
10 
11     @Test
12     public void testJJCC() {
13         HeXinCode hx=new HeXinCode();
14         int num_1=hx.JJCC('+', 3, 6);
15         assertEquals(9,num_1);
16         int num_2=hx.JJCC('-', 3, 6);
17         assertEquals(-3,num_2);
18         int num_3=hx.JJCC('*', 3, 6);
19         assertEquals(18,num_3);
20         int num_4=hx.JJCC('/', 6, 3);
21         assertEquals(2,num_4);
22         
23     }
24 
25     @Test
26     public void testJC() {
27         HeXinCode hx=new HeXinCode();
28         int num_5=hx.JC(3);
29         assertEquals(6,num_5);
30     }
31 
32 }
HeXinCodeTest

 截图:

修改前的代码(只用了一个class):

  1 import java.awt.EventQueue;
  2 import javax.swing.JFrame;
  3 import javax.swing.JPanel;
  4 import javax.swing.GroupLayout;
  5 import javax.swing.GroupLayout.Alignment;
  6 import javax.swing.LayoutStyle.ComponentPlacement;
  7 import java.awt.Font;
  8 import javax.swing.JTextPane;
  9 import javax.swing.JButton;
 10 import java.awt.Color;
 11 import javax.swing.JTextField;
 12 import javax.swing.JLabel;
 13 import java.awt.event.*;
 14 import java.util.Random;
 15 
 16 public class SiZeYunSuan {
 17 
 18     private JFrame frame;
 19     private JTextField textField;
 20     private JTextField textField_1;
 21     private JTextField textField_2;
 22     private JTextField textField_3;
 23     int num=5,T=0,F=0,count=1;
 24     int ranswer=0;                 
 25     int uanswer=0;                 
 26     private JTextField textField_4;
 27     private JTextField textField_5;
 28     private JTextField textField_6;
 29     private JTextField textField_7;
 30     private boolean stop = false;
 31     String str="";
 32     String z="";
 33     JTextPane textPane = new JTextPane();
 34     JButton button = new JButton("\u5F00\u59CB\u7B54\u9898");
 35     long startTime;
 36     long endTime;
 37     /**
 38      * Launch the application.
 39      */
 40     public static void main(String[] args) {
 41         EventQueue.invokeLater(new Runnable() {
 42             public void run() {
 43                 try {
 44                     SiZeYunSuan window = new SiZeYunSuan();
 45                     window.frame.setVisible(true);
 46                 } catch (Exception e) {
 47                     e.printStackTrace();
 48                 }
 49             }
 50         });
 51     }
 52 
 53     /**
 54      * Create the application.
 55      */
 56     public SiZeYunSuan() {
 57         initialize();    
 58     }
 59 
 60     /**
 61      * Initialize the contents of the frame.
 62      */
 63     private void initialize() {
 64         frame = new JFrame();
 65         frame.setResizable(false);
 66         
 67         JPanel panel = new JPanel();
 68         GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
 69         groupLayout.setHorizontalGroup(
 70             groupLayout.createParallelGroup(Alignment.LEADING)
 71                 .addGroup(groupLayout.createSequentialGroup()
 72                     .addComponent(panel, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
 73                     .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 74         );
 75         groupLayout.setVerticalGroup(
 76             groupLayout.createParallelGroup(Alignment.LEADING)
 77                 .addComponent(panel, GroupLayout.PREFERRED_SIZE, 487, Short.MAX_VALUE)
 78         );
 79         
 80         
 81         textPane.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 22));
 82         textPane.setEditable(false);
 83         textPane.setToolTipText("");
 84         textPane.setBackground(Color.WHITE);
 85         
 86         JPanel panel_1 = new JPanel();
 87         panel_1.setBackground(new Color(204, 204, 204));
 88         
 89         
 90         button.setBackground(Color.GREEN);
 91         
 92         JButton button_1 = new JButton("\u9000\u51FA");
 93         button_1.addActionListener(new ActionListener() {
 94             public void actionPerformed(ActionEvent e) {
 95                 System.exit(0);
 96             }
 97         });
 98         button_1.setBackground(Color.RED);
 99         
100         JLabel lblNewLabel = new JLabel(" \u8F93\u5165\u9898\u6570\uFF1A");
101         lblNewLabel.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
102         lblNewLabel.setForeground(new Color(0, 0, 0));
103         
104         JLabel label = new JLabel(" \u7B54\u5BF9\u9898\u6570\uFF1A");
105         label.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
106         
107         JLabel label_1 = new JLabel(" \u7B54\u9519\u9898\u6570\uFF1A");
108         label_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
109         
110         JLabel label_2 = new JLabel(" \u4FE1\u606F\u53CD\u9988\uFF1A");
111         label_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
112         
113         textField = new JTextField();
114         textField.setColumns(10);
115         
116         textField_1 = new JTextField();
117         textField_1.setForeground(Color.GREEN);
118         textField_1.setEditable(false);
119         textField_1.setColumns(10);
120         
121         textField_2 = new JTextField();
122         textField_2.setForeground(Color.RED);
123         textField_2.setEditable(false);
124         textField_2.setColumns(10);
125         
126         textField_3 = new JTextField();
127         textField_3.setFont(new Font("宋体", Font.PLAIN, 12));
128         textField_3.setForeground(Color.RED);
129         textField_3.setEditable(false);
130         textField_3.setColumns(10);
131         
132         JLabel label_3 = new JLabel(" \u6B63\u786E\u7B54\u6848\uFF1A");
133         label_3.setFont(new Font("Dialog", Font.PLAIN, 15));
134         
135         JLabel label_4 = new JLabel(" \u8F93\u5165\u7B54\u6848\uFF1A");
136         label_4.setFont(new Font("Dialog", Font.PLAIN, 15));
137         
138         JLabel label_5 = new JLabel(" \u5F53\u524D\u9898\u6570\uFF1A");
139         label_5.setFont(new Font("Dialog", Font.PLAIN, 15));
140         
141         textField_4 = new JTextField();
142         textField_4.setEditable(false);
143         textField_4.setColumns(10);
144         
145         textField_5 = new JTextField();
146         textField_5.setColumns(10);
147         
148         textField_6 = new JTextField();
149         textField_6.setFont(new Font("Yu Gothic", Font.BOLD, 14));
150         textField_6.setForeground(Color.CYAN);
151         textField_6.setEditable(false);
152         textField_6.setColumns(10);
153         
154         JLabel label_6 = new JLabel(" \u6240\u7528\u65F6\u95F4\uFF1A");
155         label_6.setFont(new Font("Dialog", Font.PLAIN, 15));
156         
157         textField_7 = new JTextField();
158         textField_7.setFont(new Font("宋体", Font.PLAIN, 14));
159         textField_7.setEditable(false);
160         textField_7.setColumns(10);
161         GroupLayout gl_panel_1 = new GroupLayout(panel_1);
162         gl_panel_1.setHorizontalGroup(
163             gl_panel_1.createParallelGroup(Alignment.TRAILING)
164                 .addGroup(gl_panel_1.createSequentialGroup()
165                     .addGap(11)
166                     .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
167                     .addGap(110))
168                 .addGroup(gl_panel_1.createSequentialGroup()
169                     .addContainerGap()
170                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
171                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
172                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
173                                 .addGroup(gl_panel_1.createSequentialGroup()
174                                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
175                                         .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
176                                         .addComponent(label, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
177                                     .addPreferredGap(ComponentPlacement.RELATED))
178                                 .addGroup(gl_panel_1.createSequentialGroup()
179                                     .addComponent(label_3, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
180                                     .addPreferredGap(ComponentPlacement.RELATED)))
181                             .addGroup(gl_panel_1.createSequentialGroup()
182                                 .addComponent(label_1)
183                                 .addPreferredGap(ComponentPlacement.RELATED)))
184                         .addGroup(gl_panel_1.createSequentialGroup()
185                             .addComponent(label_4)
186                             .addPreferredGap(ComponentPlacement.RELATED)))
187                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
188                         .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
189                         .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
190                         .addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
191                         .addGroup(gl_panel_1.createSequentialGroup()
192                             .addComponent(textField, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
193                             .addPreferredGap(ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
194                         .addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
195                         .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
196                     .addGap(66))
197                 .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
198                     .addContainerGap()
199                     .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
200                         .addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
201                         .addComponent(label_2, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
202                     .addPreferredGap(ComponentPlacement.RELATED)
203                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
204                         .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
205                         .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
206                         .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
207                     .addContainerGap(10, Short.MAX_VALUE))
208                 .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
209                     .addGap(52)
210                     .addComponent(button)
211                     .addContainerGap(91, Short.MAX_VALUE))
212         );
213         gl_panel_1.setVerticalGroup(
214             gl_panel_1.createParallelGroup(Alignment.LEADING)
215                 .addGroup(gl_panel_1.createSequentialGroup()
216                     .addContainerGap()
217                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
218                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
219                         .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
220                     .addGap(18)
221                     .addComponent(button, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
222                     .addGap(18)
223                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
224                         .addGroup(gl_panel_1.createSequentialGroup()
225                             .addGap(18)
226                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
227                                 .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
228                                 .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
229                             .addPreferredGap(ComponentPlacement.RELATED)
230                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
231                                 .addGroup(gl_panel_1.createSequentialGroup()
232                                     .addGap(40)
233                                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
234                                         .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
235                                         .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
236                                 .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
237                                     .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
238                                     .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
239                             .addPreferredGap(ComponentPlacement.UNRELATED)
240                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
241                                 .addComponent(label, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
242                                 .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
243                             .addPreferredGap(ComponentPlacement.RELATED)
244                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
245                                 .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
246                                 .addComponent(label_1)))
247                         .addGroup(gl_panel_1.createSequentialGroup()
248                             .addGap(206)
249                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
250                                 .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
251                                 .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))))
252                     .addPreferredGap(ComponentPlacement.RELATED)
253                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
254                         .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
255                         .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
256                     .addGap(33)
257                     .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
258                     .addGap(40))
259         );
260         panel_1.setLayout(gl_panel_1);
261         
262         button.addActionListener(new ActionListener(){//开始答题
263             public void actionPerformed(ActionEvent e)
264             {
265                 count=1;
266                 T=0;F=0;
267                 str="";
268                 z="";
269                 stop=false;
270                 textField_6.setText("");
271                 textField_4.setText(""+count);
272                 try{
273                 num=Integer.parseInt(textField.getText());
274                 if(num>5)
275                 {
276                     
277                     textPane.setText("输入题数错误,请重新输入!(1~5)");
278                     textField.setText("");
279                 }
280                 else
281                 {
282                     startTime=System.currentTimeMillis();
283                     textField_1.setText(""+T);
284                     textField_2.setText(""+F);
285                     button.setVisible(false);
286                     textField.setEditable(false);
287                     showtext();
288                 }
289                 }
290                 catch(NumberFormatException e1)
291                 {
292                     textPane.setText("输入题数错误,请重新输入!(1~5)");
293                     textField.setText("");
294                 }
295                 
296                 
297             }            
298         });
299         textField_5.addActionListener(new ActionListener(){
300             public void actionPerformed(ActionEvent e)
301             {try{
302                 if(!stop)
303                 {
304                 
305                 
306                 uanswer=Integer.parseInt(textField_5.getText());
307                 if(num==count)
308                 {
309                     if(uanswer==ranswer) 
310                     {  
311                         textField_6.setText(""+ranswer);
312                         textField_3.setText("答对!"); 
313                         T++; 
314                         textField_1.setText(""+T);    
315                         count++;    
316                     }  
317                     else 
318                     {  
319                         textField_6.setText(""+ranswer);
320                         textField_3.setText("答错!");
321                         F++;
322                         textField_2.setText(""+F);
323                         count++;
324                     }
325                     stop=true;
326                     textField_3.setText(" 答題結束!");
327                     endTime=System.currentTimeMillis();
328                     textField.setEditable(true);
329                     button.setVisible(true);
330                     textField_7.setText((endTime-startTime)/1000+"s");
331                     
332                 }
333                 else if(uanswer==ranswer) 
334                 {   
335                     textField_6.setText(""+ranswer);
336                     textField_3.setText("答对!"); 
337                     T++; 
338                     textField_1.setText(""+T);    
339                     showtext();
340                     count++;
341                     
342                 }  
343                 else
344                 {   
345                     textField_6.setText(""+ranswer);
346                     textField_3.setText("答错!");
347                     F++;
348                     textField_2.setText(""+F);
349                     showtext();
350                     count++;
351                 }
352                 }
353             }
354                 
355                 catch(NumberFormatException e2)
356                 {
357                     textPane.setText(str+"答案格式错误,请重新输入答案!");
358                     textField_5.setText("");
359                 }
360             
361             }
362         });
363         GroupLayout gl_panel = new GroupLayout(panel);
364         gl_panel.setHorizontalGroup(
365             gl_panel.createParallelGroup(Alignment.LEADING)
366                 .addGroup(gl_panel.createSequentialGroup()
367                     .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
368                     .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
369                     .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
370                     .addGap(28))
371         );
372         gl_panel.setVerticalGroup(
373             gl_panel.createParallelGroup(Alignment.LEADING)
374                 .addGroup(gl_panel.createSequentialGroup()
375                     .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
376                         .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 484, GroupLayout.PREFERRED_SIZE)
377                         .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
378                     .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
379         );
380         panel.setLayout(gl_panel);
381         frame.getContentPane().setLayout(groupLayout);
382         frame.setBounds(100, 100, 454, 518);
383         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
384         }    
385     void showtext()
386     {
387         char op='+';
388         int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
389         int temp=0;
390         Random ra =new Random();
391         e=ra.nextInt(10)+1;
392         f=ra.nextInt(10)+1;
393         chioce=ra.nextInt(2)+1;
394         if(e%2==0)
395         {
396         a=ra.nextInt(10)+2;
397         }
398         else
399         {
400             a=-ra.nextInt(10)+2;
401         }
402         if(f%2==0)
403         {
404         b=ra.nextInt(10)+2;
405         }
406         else
407         {
408             b=-ra.nextInt(10)+2;
409         }
410         c=ra.nextInt(5);
411         opnum=Math.abs(ra.nextInt())%4+1;    
412         textField_5.setText("");
413         switch(chioce)
414         {
415         case 1:
416         {
417         switch(opnum)  
418             {  
419             case 1:    
420                 op='+';  
421                 break;
422             case 2:  
423                 op='-'; 
424                 break;
425             case 3:  
426                 op='*';    
427                 break;
428             case 4:  
429                 op='/';  
430                 break; 
431             }                                               
432                                                             
433             if((op=='-') && (a<b)) 
434             {   
435                 temp=a; 
436                 a=b;
437                 b=temp;  
438             }
439                                                           
440             if(op=='/')  
441             {   
442                 if(b==0)
443                 {    
444                     b=1;  
445                 }   
446                 a=a*b; 
447             }  
448             if(a>0&&b>0)
449             {
450                 z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"\n";
451             }
452             else if(a<0&&b>0)
453             {
454                 z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"\n";
455             }
456             else if(a>0&&b<0)
457             {
458                 z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
459             }
460             else
461             {
462                 z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
463             }
464             str=str+z;                                                  
465             textPane.setText(str); 
466         
467             switch(op) 
468             {  
469             case '+':   ranswer=a+b;   break;
470             case '-':   ranswer=a-b;   break;
471             case '*':   ranswer=a*b;   break;
472             case '/':   ranswer=a/b;   break;  
473             } 
474             break;
475         }
476         case 2:
477         {
478             int jc=1;
479             str=str+String.valueOf(c)+"!="+"\n";                                                
480             textPane.setText(str);
481             for(int i=c;i>0;i--)
482             {
483                 jc=jc*i;
484             }
485             ranswer=jc;
486             break;
487         }
488         }
489     }
490 }
修改前

修改后的代码(把核心代码分开,并做出修改)

  1 package KuangJiapk;
  2 
  3 
  4 import java.awt.EventQueue;
  5 
  6 import javax.swing.JFrame;
  7 import javax.swing.JPanel;
  8 import javax.swing.GroupLayout;
  9 import javax.swing.GroupLayout.Alignment;
 10 import javax.swing.LayoutStyle.ComponentPlacement;
 11 
 12 import java.awt.Font;
 13 
 14 import javax.swing.JTextPane;
 15 import javax.swing.JButton;
 16 
 17 import java.awt.Color;
 18 
 19 import javax.swing.JTextField;
 20 import javax.swing.JLabel;
 21 
 22 import GongNengpk.HeXinCode;
 23 
 24 import java.awt.event.*;
 25 import java.util.Random;
 26 
 27 public class KuangJiaCode {
 28 
 29     private JFrame frame;
 30     private JTextField textField;
 31     private JTextField textField_1;
 32     private JTextField textField_2;
 33     private JTextField textField_3;
 34     int num=5,T=0,F=0,count=1;
 35     int ranswer=0;                 
 36     int uanswer=0;                 
 37     private JTextField textField_4;
 38     private JTextField textField_5;
 39     private JTextField textField_6;
 40     private JTextField textField_7;
 41     private boolean stop = false;
 42     String str="";
 43     String z="";
 44     JTextPane textPane = new JTextPane();
 45     JButton button = new JButton("\u5F00\u59CB\u7B54\u9898");
 46     long startTime;
 47     long endTime;
 48     /**
 49      * Launch the application.
 50      */
 51     public static void main(String[] args) {
 52         EventQueue.invokeLater(new Runnable() {
 53             public void run() {
 54                 try {
 55                     KuangJiaCode window = new KuangJiaCode();
 56                     window.frame.setVisible(true);
 57                 } catch (Exception e) {
 58                     e.printStackTrace();
 59                 }
 60             }
 61         });
 62     }
 63 
 64     /**
 65      * Create the application.
 66      */
 67     public KuangJiaCode() {
 68         initialize();    
 69     }
 70 
 71     /**
 72      * Initialize the contents of the frame.
 73      */
 74     private void initialize() {
 75         frame = new JFrame();
 76         frame.setResizable(false);
 77         
 78         JPanel panel = new JPanel();
 79         GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
 80         groupLayout.setHorizontalGroup(
 81             groupLayout.createParallelGroup(Alignment.LEADING)
 82                 .addGroup(groupLayout.createSequentialGroup()
 83                     .addComponent(panel, GroupLayout.PREFERRED_SIZE, 448, GroupLayout.PREFERRED_SIZE)
 84                     .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
 85         );
 86         groupLayout.setVerticalGroup(
 87             groupLayout.createParallelGroup(Alignment.LEADING)
 88                 .addComponent(panel, GroupLayout.PREFERRED_SIZE, 487, Short.MAX_VALUE)
 89         );
 90         
 91         
 92         textPane.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 22));
 93         textPane.setEditable(false);
 94         textPane.setToolTipText("");
 95         textPane.setBackground(Color.WHITE);
 96         
 97         JPanel panel_1 = new JPanel();
 98         panel_1.setBackground(new Color(204, 204, 204));
 99         
100         
101         button.setBackground(Color.GREEN);
102         
103         JButton button_1 = new JButton("\u9000\u51FA");
104         button_1.addActionListener(new ActionListener() {
105             public void actionPerformed(ActionEvent e) {
106                 System.exit(0);
107             }
108         });
109         button_1.setBackground(Color.RED);
110         
111         JLabel lblNewLabel = new JLabel(" \u8F93\u5165\u9898\u6570\uFF1A");
112         lblNewLabel.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
113         lblNewLabel.setForeground(new Color(0, 0, 0));
114         
115         JLabel label = new JLabel(" \u7B54\u5BF9\u9898\u6570\uFF1A");
116         label.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
117         
118         JLabel label_1 = new JLabel(" \u7B54\u9519\u9898\u6570\uFF1A");
119         label_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
120         
121         JLabel label_2 = new JLabel(" \u4FE1\u606F\u53CD\u9988\uFF1A");
122         label_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 15));
123         
124         textField = new JTextField();
125         textField.setColumns(10);
126         
127         textField_1 = new JTextField();
128         textField_1.setForeground(Color.GREEN);
129         textField_1.setEditable(false);
130         textField_1.setColumns(10);
131         
132         textField_2 = new JTextField();
133         textField_2.setForeground(Color.RED);
134         textField_2.setEditable(false);
135         textField_2.setColumns(10);
136         
137         textField_3 = new JTextField();
138         textField_3.setFont(new Font("宋体", Font.PLAIN, 12));
139         textField_3.setForeground(Color.RED);
140         textField_3.setEditable(false);
141         textField_3.setColumns(10);
142         
143         JLabel label_3 = new JLabel(" \u6B63\u786E\u7B54\u6848\uFF1A");
144         label_3.setFont(new Font("Dialog", Font.PLAIN, 15));
145         
146         JLabel label_4 = new JLabel(" \u8F93\u5165\u7B54\u6848\uFF1A");
147         label_4.setFont(new Font("Dialog", Font.PLAIN, 15));
148         
149         JLabel label_5 = new JLabel(" \u5F53\u524D\u9898\u6570\uFF1A");
150         label_5.setFont(new Font("Dialog", Font.PLAIN, 15));
151         
152         textField_4 = new JTextField();
153         textField_4.setEditable(false);
154         textField_4.setColumns(10);
155         
156         textField_5 = new JTextField();
157         textField_5.setColumns(10);
158         
159         textField_6 = new JTextField();
160         textField_6.setFont(new Font("Yu Gothic", Font.BOLD, 14));
161         textField_6.setForeground(Color.CYAN);
162         textField_6.setEditable(false);
163         textField_6.setColumns(10);
164         
165         JLabel label_6 = new JLabel(" \u6240\u7528\u65F6\u95F4\uFF1A");
166         label_6.setFont(new Font("Dialog", Font.PLAIN, 15));
167         
168         textField_7 = new JTextField();
169         textField_7.setFont(new Font("宋体", Font.PLAIN, 14));
170         textField_7.setEditable(false);
171         textField_7.setColumns(10);
172         GroupLayout gl_panel_1 = new GroupLayout(panel_1);
173         gl_panel_1.setHorizontalGroup(
174             gl_panel_1.createParallelGroup(Alignment.TRAILING)
175                 .addGroup(gl_panel_1.createSequentialGroup()
176                     .addGap(11)
177                     .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
178                     .addGap(110))
179                 .addGroup(gl_panel_1.createSequentialGroup()
180                     .addContainerGap()
181                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
182                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
183                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
184                                 .addGroup(gl_panel_1.createSequentialGroup()
185                                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
186                                         .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
187                                         .addComponent(label, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
188                                     .addPreferredGap(ComponentPlacement.RELATED))
189                                 .addGroup(gl_panel_1.createSequentialGroup()
190                                     .addComponent(label_3, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
191                                     .addPreferredGap(ComponentPlacement.RELATED)))
192                             .addGroup(gl_panel_1.createSequentialGroup()
193                                 .addComponent(label_1)
194                                 .addPreferredGap(ComponentPlacement.RELATED)))
195                         .addGroup(gl_panel_1.createSequentialGroup()
196                             .addComponent(label_4)
197                             .addPreferredGap(ComponentPlacement.RELATED)))
198                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
199                         .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
200                         .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
201                         .addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
202                         .addGroup(gl_panel_1.createSequentialGroup()
203                             .addComponent(textField, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
204                             .addPreferredGap(ComponentPlacement.RELATED, 13, Short.MAX_VALUE))
205                         .addComponent(textField_5, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
206                         .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE))
207                     .addGap(66))
208                 .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
209                     .addContainerGap()
210                     .addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING, false)
211                         .addComponent(label_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
212                         .addComponent(label_2, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
213                     .addPreferredGap(ComponentPlacement.RELATED)
214                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
215                         .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
216                         .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
217                         .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
218                     .addContainerGap(10, Short.MAX_VALUE))
219                 .addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
220                     .addGap(52)
221                     .addComponent(button)
222                     .addContainerGap(91, Short.MAX_VALUE))
223         );
224         gl_panel_1.setVerticalGroup(
225             gl_panel_1.createParallelGroup(Alignment.LEADING)
226                 .addGroup(gl_panel_1.createSequentialGroup()
227                     .addContainerGap()
228                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
229                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
230                         .addComponent(textField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
231                     .addGap(18)
232                     .addComponent(button, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
233                     .addGap(18)
234                     .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
235                         .addGroup(gl_panel_1.createSequentialGroup()
236                             .addGap(18)
237                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
238                                 .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
239                                 .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
240                             .addPreferredGap(ComponentPlacement.RELATED)
241                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
242                                 .addGroup(gl_panel_1.createSequentialGroup()
243                                     .addGap(40)
244                                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
245                                         .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
246                                         .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
247                                 .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
248                                     .addComponent(textField_5, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
249                                     .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)))
250                             .addPreferredGap(ComponentPlacement.UNRELATED)
251                             .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
252                                 .addComponent(label, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
253                                 .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
254                             .addPreferredGap(ComponentPlacement.RELATED)
255                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
256                                 .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
257                                 .addComponent(label_1)))
258                         .addGroup(gl_panel_1.createSequentialGroup()
259                             .addGap(206)
260                             .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
261                                 .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
262                                 .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))))
263                     .addPreferredGap(ComponentPlacement.RELATED)
264                     .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
265                         .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
266                         .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
267                     .addGap(33)
268                     .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
269                     .addGap(40))
270         );
271         panel_1.setLayout(gl_panel_1);
272         GroupLayout gl_panel = new GroupLayout(panel);
273         gl_panel.setHorizontalGroup(
274             gl_panel.createParallelGroup(Alignment.LEADING)
275                 .addGroup(gl_panel.createSequentialGroup()
276                     .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 243, GroupLayout.PREFERRED_SIZE)
277                     .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
278                     .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
279                     .addGap(28))
280         );
281         gl_panel.setVerticalGroup(
282             gl_panel.createParallelGroup(Alignment.LEADING)
283                 .addGroup(gl_panel.createSequentialGroup()
284                     .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
285                         .addComponent(textPane, GroupLayout.PREFERRED_SIZE, 484, GroupLayout.PREFERRED_SIZE)
286                         .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
287                     .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
288         );
289         panel.setLayout(gl_panel);
290         frame.getContentPane().setLayout(groupLayout);
291         frame.setBounds(100, 100, 454, 518);
292         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
293             
294         
295         button.addActionListener(new ActionListener(){//开始答题
296             public void actionPerformed(ActionEvent e)
297             {
298                 count=1;
299                 T=0;F=0;
300                 str="";
301                 z="";
302                 stop=false;
303                 textField_6.setText("");
304                 textField_4.setText(""+count);
305                 try{
306                 num=Integer.parseInt(textField.getText());
307                 if(num>5)
308                 {
309                     
310                     textPane.setText("输入题数错误,请重新输入!(1~5)");
311                     textField.setText("");
312                 }
313                 else
314                 {
315                     startTime=System.currentTimeMillis();
316                     textField_1.setText(""+T);
317                     textField_2.setText(""+F);
318                     button.setVisible(false);
319                     textField.setEditable(false);
320                     showtext();
321                 }
322                 }
323                 catch(NumberFormatException e1)
324                 {
325                     textPane.setText("输入题数错误,请重新输入!(1~5)");
326                     textField.setText("");
327                 }
328                 
329                 
330             }            
331         });
332         textField_5.addActionListener(new ActionListener(){
333             public void actionPerformed(ActionEvent e)
334             {try{
335                 if(!stop)
336                 {
337                 
338                 
339                 uanswer=Integer.parseInt(textField_5.getText());
340                 if(num==count)
341                 {
342                     if(uanswer==ranswer) 
343                     {  
344                         textField_6.setText(""+ranswer);
345                         textField_3.setText("答对!"); 
346                         T++; 
347                         textField_1.setText(""+T);    
348                         count++;    
349                     }  
350                     else 
351                     {  
352                         textField_6.setText(""+ranswer);
353                         textField_3.setText("答错!");
354                         F++;
355                         textField_2.setText(""+F);
356                         count++;
357                     }
358                     stop=true;
359                     textField_3.setText(" 答題結束!");
360                     endTime=System.currentTimeMillis();
361                     textField.setEditable(true);
362                     button.setVisible(true);
363                     textField_7.setText((endTime-startTime)/1000+"s");
364                     
365                 }
366                 else if(uanswer==ranswer) 
367                 {   
368                     textField_6.setText(""+ranswer);
369                     textField_3.setText("答对!"); 
370                     T++; 
371                     textField_1.setText(""+T);    
372                     showtext();
373                     count++;
374                     
375                 }  
376                 else
377                 {   
378                     textField_6.setText(""+ranswer);
379                     textField_3.setText("答错!");
380                     F++;
381                     textField_2.setText(""+F);
382                     showtext();
383                     count++;
384                 }
385                 }
386             }
387                 
388                 catch(NumberFormatException e2)
389                 {
390                     textPane.setText(str+"答案格式错误,请重新输入答案!");
391                     textField_5.setText("");
392                 }
393             
394             }
395         });
396     }
397         
398     void showtext()
399     {
400         char op='+';
401         int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
402         int temp=0;
403         Random ra =new Random();
404         HeXinCode hx=new HeXinCode();
405         e=ra.nextInt(10)+1;
406         f=ra.nextInt(10)+1;
407         chioce=ra.nextInt(2)+1;
408         if(e%2==0)
409         {
410         a=ra.nextInt(10)+2;
411         }
412         else
413         {
414             a=-ra.nextInt(10)+2;
415         }
416         if(f%2==0)
417         {
418         b=ra.nextInt(10)+2;
419         }
420         else
421         {
422             b=-ra.nextInt(10)+2;
423         }
424         c=ra.nextInt(5);
425         opnum=Math.abs(ra.nextInt())%4+1;    
426         textField_5.setText("");
427         switch(chioce)
428         {
429         case 1:
430         {
431         switch(opnum)  
432             {  
433             case 1:    
434                 op='+';  
435                 break;
436             case 2:  
437                 op='-'; 
438                 break;
439             case 3:  
440                 op='*';    
441                 break;
442             case 4:  
443                 op='/';  
444                 break; 
445             }                                               
446                                                             
447             if((op=='-') && (a<b)) 
448             {   
449                 temp=a; 
450                 a=b;
451                 b=temp;  
452             }
453                                                           
454             if(op=='/')  
455             {   
456                 if(b==0)
457                 {    
458                     b=1;  
459                 }   
460                 a=a*b; 
461             }  
462             if(a>=0&&b>=0)
463             {
464                 z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"\n";
465             }
466             else if(a<0&&b>0)
467             {
468                 z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"\n";
469             }
470             else if(a>0&&b<0)
471             {
472                 z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
473             }
474             else
475             {
476                 z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
477             }
478             str=str+z;                                                  
479             textPane.setText(str); 
480             ranswer=hx.JJCC(op, a, b);
481             break;
482         }
483         case 2:
484         {
485             str=str+String.valueOf(c)+"!="+"\n";                                                
486             textPane.setText(str);
487             ranswer=hx.JC(c);
488             break;
489         }
490         }
491     }
492 }
KuangJia Code
 1 package GongNengpk;
 2 
 3 public class HeXinCode {
 4 
 5     int temp=0;
 6     int answer=0;
 7     public int JJCC(char op,int a,int b)
 8     {
 9         switch(op) 
10         {  
11         case '+':   answer=a+b;   break;
12         case '-':   answer=a-b;   break;
13         case '*':   answer=a*b;   break;
14         case '/':   answer=a/b;   break;  
15         } 
16         return answer;
17     }
18     public int JC(int c)
19     {
20         int jc=1;
21         for(int i=c;i>0;i--)
22         {
23             jc=jc*i;
24         }
25         answer=jc;
26         return answer;
27     }
28     
29 }
HeXin Code

 

 

总结:
       本次作业按老师的要求换了队友(http://www.cnblogs.com/yuhancheng/)。这次作业真的是一个很大的挑战。挑战了之前一直没有做过了的东西。一开始,老师叫我做这个封装、黑盒、白盒测试我是拒绝的,因为不能叫我做我就做嘛,但是我真的不会做啊~~~上课时老师说这些内容是真的是听不懂,更不要说做了。可是通过向老师提问和学习网上的教程,然后就渐渐地明白了。我的理解是,白盒测试可以让我们更清晰这个程序的思维,黑盒测试是为了帮助我们发现程序中的bug,让我们加以改善。其实还是不够了解,哈哈。只能通过更多的方法去加深对单元测试的理解。

posted on 2015-04-30 18:38  Alenqifeng  阅读(289)  评论(2编辑  收藏  举报

导航