四則運算,測試與封裝。

38鄭勝斌發表於2015-05-05

測試與封裝 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("\u56DB\u5219\u8FD0\u7B97\u56685.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("\u505A\u5B8C\u4E86");
        btnNewButton.setFont(new Font("微軟雅黑", Font.PLAIN, 30));
        btnNewButton.setBounds(188, 156, 131, 45);
        panel.add(btnNewButton);
        
        JLabel lblNewLabel_1 = new JLabel("\u8BA1\u7B97\u7ED3\u679C");
        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("\u518D\u6765 ");
        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("\n錯誤!  ,請你輸入一個整數");
    }*/
    if(answer==c)
    {
        System.out.print("答對了");
    }
    else System.out.print("答錯了");
    //System.out.print("answer="+answer);
}
}

Expression

  

個人體會:

其實一開始老師說要做封裝,我是拒絕的,但為了期末成績,我還是蠻拼的......

這次實驗要找新的夥伴,我就想到了小強童鞋。之前就想找他合作了,這次終於有了個機會和他合作了。

一開始和小強童鞋討論的時候,討論了一個晚上,我終於弄懂了他的四則運算的程式碼,還有讓我做的內容。然後到了第二天他過來宿舍說,昨天那個先放下,晚上把封裝搞好,然後一個晚上在我宿舍兩個人一臺機,慢慢把封裝和測試那些搞好了。和小強合作很有趣,我們都有不同的想法,小強的能力要比我好一點,還好我們遇到問題後都能協調解決好,這一點我覺得是比較好的。小強經常帶我裝逼帶我飛,但是我會好好學習的,就像這次,我學到了很到東西。

我相信在接下來的合作中,我們會做得更好,小強,一起努力吧!哈哈哈....

 

 

相關文章