zoukankan      html  css  js  c++  java
  • 四则运算,测试与封装。

    测试与封装 5.1

    程序开发简介:

    【开发环境】:eclipse

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

    【博客地址】:http://www.cnblogs.com/IvesHe/

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

    【版本】:5.1

    【要求】:

    • 封装
    • 测试

    【分工】:

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

          郑胜斌:封装 Expression类。

    封装:

    概念

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

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

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

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

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

    步骤: 

        1、修改属性的可见性来限制对属性的访问。(通常将类的成员变量声明为private)
          2、为每个属性创建一对赋值方法和取值方法,用于对这些属性的访问。
          3、在赋值和取值方法中,加入对属性的存取限制。
     
    单元测试小白式教程:
     
     
     
    Ives:
    登陆界面
    package com.ives;
    
    import java.awt.EventQueue;
    
    public class frame {
    
        private JFrame frame;
        private JTextField textField;
        private JTextField textField_1;
        private JTextField textField_2;
        private JTextField textField_3;
        private JTextField textField_4;
        private JTextField textField_5;
        
        int a;
        int b;
        int op;
        String Sa;
        String Sb;
        int result;
        Expression expression = new Expression();
        private JButton btnNewButton_1;
    
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        frame window = new frame();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         * @throws Yichang 
         */
        public frame() throws Yichang {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() throws Yichang{
            frame = new JFrame();
            frame.setBounds(100, 100, 517, 352);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JPanel panel = new JPanel();
            frame.getContentPane().add(panel, BorderLayout.CENTER);
            panel.setLayout(null);
            
            JLabel lblNewLabel = new JLabel("u56DBu5219u8FD0u7B97u56685.1");
            lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            lblNewLabel.setBounds(141, 0, 208, 57);
            panel.add(lblNewLabel);
            
            textField = new JTextField();
            textField.setHorizontalAlignment(SwingConstants.CENTER);
            textField.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField.setBounds(56, 82, 88, 45);
            panel.add(textField);
            textField.setColumns(10);
            
            textField_1 = new JTextField();
            textField_1.setHorizontalAlignment(SwingConstants.CENTER);
            textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField_1.setColumns(10);
            textField_1.setBounds(220, 82, 88, 45);
            panel.add(textField_1);
            
            textField_2 = new JTextField("=");
            textField_2.setEditable(false);
            textField_2.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField_2.setColumns(10);
            textField_2.setBounds(318, 82, 36, 45);
            panel.add(textField_2);
            
            textField_3 = new JTextField();
            textField_3.setHorizontalAlignment(SwingConstants.CENTER);
            textField_3.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField_3.setColumns(10);
            textField_3.setBounds(371, 82, 88, 45);
            panel.add(textField_3);
            
            JButton btnNewButton = new JButton("u505Au5B8Cu4E86");
            btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            btnNewButton.setBounds(188, 156, 131, 45);
            panel.add(btnNewButton);
            
            JLabel lblNewLabel_1 = new JLabel("u8BA1u7B97u7ED3u679C");
            lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            lblNewLabel_1.setBounds(56, 222, 131, 35);
            panel.add(lblNewLabel_1);
            
            textField_4 = new JTextField();
            textField_4.setFont(new Font("微软雅黑", Font.PLAIN, 20));
            textField_4.setColumns(10);
            textField_4.setBounds(188, 222, 161, 35);
            panel.add(textField_4);
            
            textField_5 = new JTextField();
            textField_5.setEditable(false);
            textField_5.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            textField_5.setText("+");
            textField_5.setColumns(10);
            textField_5.setBounds(159, 82, 44, 45);
            panel.add(textField_5);
            
            a = expression.geta();
            Sa = String.valueOf(a);
            textField.setText(Sa);
            
            b = expression.getb();
            Sb = String.valueOf(b);
            textField_1.setText(Sb);
            
            btnNewButton_1 = new JButton("u518Du6765 ");
            btnNewButton_1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    
                }
            });
            btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 30));
            btnNewButton_1.setBounds(366, 222, 107, 36);
            panel.add(btnNewButton_1);
            
    
            btnNewButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0){
                    int n3=Integer.valueOf(textField_3.getText().toString());
                    result = expression.getaswer();
                    if(textField_3.getText().equals(""))
                    {
                        String inputValue = JOptionPane.showInputDialog("Please input a value");
                        n3 = Integer.parseInt(inputValue);
                    }     
                    try {
                     if(n3<0)
                    
                            throw new Yichang("不可能是负数!");
                        } catch (Yichang e) {
                            // TODO 自动生成的 catch 块
                            e.printStackTrace();
                        }
                     if(result==n3)
                    {
                        textField_4.setText(" 正确");
                    }else{
                        textField_4.setText(" 错误!答案为"+result);
                    }
                    }
            });
        }
    }
    
    frame
    

      自定义异常代码

    package com.ives;
    
    class Yichang extends Exception 
    {  
        public Yichang(String msg)      
        {  
            super(msg);  
        }  
    }  
    
    Yichang
    

      测试代码如下:

    package com.ives;
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    
    public class ExpressionTest {
    
        @Test
        public void testExpression() {
            int a;
            Expression test = new Expression();
            a = test.getaswer();
            assertEquals(a, test.answer);
        } 
    }
    
    ExpressionTest
    

      

    郑胜斌 

    计算代码如下:
    package com.ives;
    import java.util.*;
    
    import com.ives.Input;
    public class Expression 
    {
        private int a = (int)(Math.random() * Math.pow(10,2)-1);
        private int b = (int)(Math.random() * Math.pow(10,2)-1);
        private int op;
        static int c;//用户答案
        int answer;//答案
        static Scanner in=new Scanner(System.in);
        
    public int geta()
    {
        return a;
    }
    
    public void seta(int a)
    {
        this.a = a;
    }
    
    public int getb()
    {
        return b;
    }
    
    public void setb(int b)
    {
        this.b = b;
    }
    
    public int getaswer()
    {
        return answer;
    }
    public Expression()
    {
        answer = a+b;
    }
    public static void main(String[] args){
        int answer;
        Expression expression = new Expression();
        
        answer = expression.answer;
        Input input = new Input();
        Expression.c = input.a;
        /*try{
            Expression.c = in.nextInt();
        }
        catch(InputMismatchException e)
        {   System.err.println("
    错误!  ,请你输入一个整数");
        }*/
        if(answer==c)
        {
            System.out.print("答对了");
        }
        else System.out.print("答错了");
        //System.out.print("answer="+answer);
    }
    }
    
    Expression
    

      

    个人体会:

    其实一开始老师说要做封装,我是拒绝的,但为了期末成绩,我还是蛮拼的......

    这次实验要找新的伙伴,我就想到了小强童鞋。之前就想找他合作了,这次终于有了个机会和他合作了。

    一开始和小强童鞋讨论的时候,讨论了一个晚上,我终于弄懂了他的四则运算的代码,还有让我做的内容。然后到了第二天他过来宿舍说,昨天那个先放下,晚上把封装搞好,然后一个晚上在我宿舍两个人一台机,慢慢把封装和测试那些搞好了。和小强合作很有趣,我们都有不同的想法,小强的能力要比我好一点,还好我们遇到问题后都能协调解决好,这一点我觉得是比较好的。小强经常带我装逼带我飞,但是我会好好学习的,就像这次,我学到了很到东西。

    我相信在接下来的合作中,我们会做得更好,小强,一起努力吧!哈哈哈....

  • 相关阅读:
    时间插件--daterangepicker使用和配置详解
    AdminLTE 前端框架
    vue element-ui 绑定@keyup事件无效
    Plugin/Preset files are not allowed to export objects,webpack报错/babel报错的解决方法
    webpack学习
    指令
    【Nuxt】配置路由
    【面试】常见面试题
    安装虚拟机
    【linux】基础知识
  • 原文地址:https://www.cnblogs.com/zsb1/p/4479247.html
Copyright © 2011-2022 走看看