【作业报告】作业5 四则运算 测试与封装 5.1

 测试与封装 5.1

程序开发简介:

【开发环境】:eclipse

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

【博客地址】:38郑胜斌

【开发时间】:2015-04-30

【版本】:5.1

【要求】:

  • 封装
  • 测试

 

 

【分工】:

      Ives:单元测试。界面。自定义异常。

      郑胜斌:封装 Expression类。

封装:

概念

  封装是把过程和数据包围起来,对数据的访问只能通过已定义的接口。面向对象计算始于这个基本概念,即现实世界可以被描绘成一系列完全自治、封装的对象,这些对象通过一个受保护的接口访问其他对象。封装是一种信息隐藏技术,在java中通过关键字private实现封装。什么是封装?封装把对象的所有组成部分组合在一起,封装定义程序如何引用对象的数据,封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度。
      
作用

    1、良好的封装能够减少耦合。

    2、类内部的结构可以自由修改。

    3、可以对成员进行更精确的控制。

    4、隐藏信息,实现细节。

步骤: 

    1、修改属性的可见性来限制对属性的访问。(通常将类的成员变量声明为private)
      2、为每个属性创建一对赋值方法和取值方法,用于对这些属性的访问。
      3、在赋值和取值方法中,加入对属性的存取限制。
 
单元测试小白式教程:
 
这次我是和1班的郑胜斌童鞋一起结对写这个程序的。这是第一个阶段,第一个阶段还是比较简单的,估计接下来有得忙一会了。废话不多说了,今天些了一天的博客了,这篇我尽量简洁些好了。
 
Ives
 
界面代码如下:
  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     
 15     int a;
 16     int b;
 17     int op;
 18     String Sa;
 19     String Sb;
 20     int result;
 21     Expression expression = new Expression();
 22     private JButton btnNewButton_1;
 23 
 24 
 25     /**
 26      * Launch the application.
 27      */
 28     public static void main(String[] args) {
 29         EventQueue.invokeLater(new Runnable() {
 30             public void run() {
 31                 try {
 32                     frame window = new frame();
 33                     window.frame.setVisible(true);
 34                 } catch (Exception e) {
 35                     e.printStackTrace();
 36                 }
 37             }
 38         });
 39     }
 40 
 41     /**
 42      * Create the application.
 43      * @throws Yichang 
 44      */
 45     public frame() throws Yichang {
 46         initialize();
 47     }
 48 
 49     /**
 50      * Initialize the contents of the frame.
 51      */
 52     private void initialize() throws Yichang{
 53         frame = new JFrame();
 54         frame.setBounds(100, 100, 517, 352);
 55         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 56         
 57         JPanel panel = new JPanel();
 58         frame.getContentPane().add(panel, BorderLayout.CENTER);
 59         panel.setLayout(null);
 60         
 61         JLabel lblNewLabel = new JLabel("\u56DB\u5219\u8FD0\u7B97\u56685.1");
 62         lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 63         lblNewLabel.setBounds(141, 0, 208, 57);
 64         panel.add(lblNewLabel);
 65         
 66         textField = new JTextField();
 67         textField.setHorizontalAlignment(SwingConstants.CENTER);
 68         textField.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 69         textField.setBounds(56, 82, 88, 45);
 70         panel.add(textField);
 71         textField.setColumns(10);
 72         
 73         textField_1 = new JTextField();
 74         textField_1.setHorizontalAlignment(SwingConstants.CENTER);
 75         textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 76         textField_1.setColumns(10);
 77         textField_1.setBounds(220, 82, 88, 45);
 78         panel.add(textField_1);
 79         
 80         textField_2 = new JTextField("=");
 81         textField_2.setEditable(false);
 82         textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 83         textField_2.setColumns(10);
 84         textField_2.setBounds(318, 82, 36, 45);
 85         panel.add(textField_2);
 86         
 87         textField_3 = new JTextField();
 88         textField_3.setHorizontalAlignment(SwingConstants.CENTER);
 89         textField_3.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 90         textField_3.setColumns(10);
 91         textField_3.setBounds(371, 82, 88, 45);
 92         panel.add(textField_3);
 93         
 94         JButton btnNewButton = new JButton("\u505A\u5B8C\u4E86");
 95         btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 30));
 96         btnNewButton.setBounds(188, 156, 131, 45);
 97         panel.add(btnNewButton);
 98         
 99         JLabel lblNewLabel_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");
100         lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
101         lblNewLabel_1.setBounds(56, 222, 131, 35);
102         panel.add(lblNewLabel_1);
103         
104         textField_4 = new JTextField();
105         textField_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));
106         textField_4.setColumns(10);
107         textField_4.setBounds(188, 222, 161, 35);
108         panel.add(textField_4);
109         
110         textField_5 = new JTextField();
111         textField_5.setEditable(false);
112         textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 30));
113         textField_5.setText("+");
114         textField_5.setColumns(10);
115         textField_5.setBounds(159, 82, 44, 45);
116         panel.add(textField_5);
117         
118         a = expression.geta();
119         Sa = String.valueOf(a);
120         textField.setText(Sa);
121         
122         b = expression.getb();
123         Sb = String.valueOf(b);
124         textField_1.setText(Sb);
125         
126         btnNewButton_1 = new JButton("\u518D\u6765 ");
127         btnNewButton_1.addActionListener(new ActionListener() {
128             public void actionPerformed(ActionEvent e) {
129                 
130             }
131         });
132         btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
133         btnNewButton_1.setBounds(366, 222, 107, 36);
134         panel.add(btnNewButton_1);
135         
136 
137         btnNewButton.addActionListener(new ActionListener() {
138             public void actionPerformed(ActionEvent arg0){
139                 int n3=Integer.valueOf(textField_3.getText().toString());
140                 result = expression.getaswer();
141                 if(textField_3.getText().equals(""))
142                 {
143                     String inputValue = JOptionPane.showInputDialog("Please input a value");
144                     n3 = Integer.parseInt(inputValue);
145                 }     
146                 try {
147                  if(n3<0)
148                 
149                         throw new Yichang("不可能是负数!");
150                     } catch (Yichang e) {
151                         // TODO 自动生成的 catch 块
152                         e.printStackTrace();
153                     }
154                  if(result==n3)
155                 {
156                     textField_4.setText(" 正确");
157                 }else{
158                     textField_4.setText(" 错误!答案为"+result);
159                 }
160                 }
161         });
162     }
163 }
frame

 

自定义异常代码如下: 
1 package com.ives;
2 
3 class Yichang extends Exception 
4 {  
5     public Yichang(String msg)      
6     {  
7         super(msg);  
8     }  
9 }  
Yichang

 

测试代码如下:

 1 package com.ives;
 2 import static org.junit.Assert.*;
 3 
 4 import org.junit.Test;
 5 
 6 
 7 public class ExpressionTest {
 8 
 9     @Test
10     public void testExpression() {
11         int a;
12         Expression test = new Expression();
13         a = test.getaswer();
14         assertEquals(a, test.answer);
15     } 
16 }
ExpressionTest

 

 

郑胜斌 

计算代码如下:
 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     static Scanner in=new Scanner(System.in);
13     
14 public int geta()
15 {
16     return a;
17 }
18 
19 public void seta(int a)
20 {
21     this.a = a;
22 }
23 
24 public int getb()
25 {
26     return b;
27 }
28 
29 public void setb(int b)
30 {
31     this.b = b;
32 }
33 
34 public int getaswer()
35 {
36     return answer;
37 }
38 public Expression()
39 {
40     answer = a+b;
41 }
42 public static void main(String[] args){
43     int answer;
44     Expression expression = new Expression();
45     
46     answer = expression.answer;
47     Input input = new Input();
48     Expression.c = input.a;
49     /*try{
50         Expression.c = in.nextInt();
51     }
52     catch(InputMismatchException e)
53     {   System.err.println("\n错误!  ,请你输入一个整数");
54     }*/
55     if(answer==c)
56     {
57         System.out.print("答对了");
58     }
59     else System.out.print("答错了");
60     //System.out.print("answer="+answer);
61 }
62 }
Expression

 

运行截图如下:

 

 

 

个人体会:

  终于到了这个版块了。每次写作业报告我都特别喜欢这个版块,能够畅所欲言,为自己代言。哈哈,废话不多说了,还是说点有用的吧。

  Duang!

  1、前几天和郑童鞋做宿舍里面讨论何谓封装,何谓单元测试,要怎么样去做这次作业的第一个阶段的。虽然讨论完了都断网了,不能玩游戏。但是,真的有充实的感觉,没有玩游戏也没关系,因为学习到了东西。而且和郑童鞋一起讨论交流感觉很愉快,没有争吵,不过不知道下个阶段会不会争吵,不过为了知识而争吵,也是蛮不错的,很期待,到时候专门更新“做汉堡”来给郑童鞋做个好吃的汉堡好了。等着我,小斌斌。

  2、知道今天,我才发现了其实我们之前讨论的封装没有讨论到它的核心思想。前面都有提到封装的概念、作用、步骤等等,我就不多说了。我觉得封装就是把属性隐藏,不对外分享。外部要访问只能通过方法。所以每个属性都应该有一对get,set方法。就好比如你有一座房子,房子里面的装饰,家具什么的都随你摆放,别人不能够懂你的任何东西,因为他们没有钥匙打开你的门,但是他们可以通过窗口来看到你里面的摆设。所以一定得留个窗口给别人,不然就会没意义了哦。

ps: 其实封装的概念很广的,助教的那个Code类有争议,有人说没有信息隐藏(没有定义私有化的变量),所以严格不算是封装,但是有人说它定义了对外接口(公共方法),这样也算是封装。嗯,不用纠结概念的。过几天贴一篇关于“封装”的博文出来,有兴趣的小伙伴可以关注哦!

  3、自定义异常类很好玩啊。哈哈。第三点我只是想表达这个!至于怎么去定义自定义类什么的教程,请看

 

     Java自定义异常 

  4.好充实的一天,写了一天的博客。痛并快乐着。

  5.对小斌斌说的话:下一个阶段的工作可能会复杂那么一点,但是我相信你,相信我们,相信我。我们会做得更好的,一起加油。

感觉这次的作业报告废话好多啊。哇哈哈。Happying~~~

 

 

 

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

 

--------如果以上有什么错误或者不足之处,欢迎指出。--------

 

PS:长期招募小伙伴一起组成一个团队学习软件开发(java), 可以合作开发软件,也可以讨论交流,有志同道合的小伙伴可以发站内消息。

 

 

posted @ 2015-04-30 18:35  苏亦晨  阅读(439)  评论(5编辑  收藏  举报