实验三 敏捷开发与XP实践

北京电子科技学院(BESTI)

             

课程:Java程序设计                     班级:1352           

姓名:闫佳歆 (结对合作:贾瑗)

学号:20135202(20135236)

成绩:             指导教师:娄佳鹏       实验日期:2015.6.2

实验密级:         预习程度:             实验时间:15:30-18:00

仪器组次:          必修/选修:必修        实验序号:3

实验名称:敏捷开发与XP实践                                                           

实验目的与要求:

实验内容

1.没有Linux基础的同学建议先学习《Linux基础入门(新版)》《Vim编辑器》课程

2.完成实验、撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等)、解决办法(空洞的方法如“查网络”、“问同学”、“看书”等一律得0分)以及分析(从中可以得到什么启示,有什么收获,教训等)。报告可以参考范飞龙老师的指导

3. 严禁抄袭,有该行为者实验成绩归零,并附加其他惩罚措施。

【实验仪器】

名称

型号

数量

PC

TOSHIBA

1

虚拟机

实验楼

1

 

【实验内容】

(一) XP基础

(二) XP核心实践

(三) 相关工具

(四)实践项目

1. 以结对编程的方式编写一个软件,Blog中要给出结对同学的Blog网址,可以拍照展现结对编程情况,可以参考一下其他学校的作业

2.记录TDD和重构的过程,测试代码不要少于业务代码,Eclipse中refactor菜单下的重构技能不要少于5个

3.团队代码要使用git在实验楼中托管,要使用结对同学中的一个同学的账号托管。

4. 程序要有GUI界面,参考用户界面和用户体验

5.程序功能从豌豆荚游戏中选择一款用Java实现,注意:团队之间项目不能有重复,课代表协调一下。

6.实验报告中统计自己的PSP(Personal Software Process)时间

参考资料

1.《解析极限编程》

2.《构建之法 (电子版)》,著者邹欣Blog

3.《结对编程技术》

4.《版本控制之道》

5.《重构》 6.《重构与模式》 7.《程序设计实践》

 

【实验步骤】

 1.程序语言风格

缩进:

 

 

根据层次空行:

 

二.git的使用

例子:

 

 

 

 

 

提交:

 

结果显示:

修改后:

 

 

三.重构

1.重构类名

 

重构函数名

其他重构方法:

 

 

 

 

 

 

前面的代码中不小心把这一行打错了,因为即做即截图所以没有来得及修改。后面的修改过了。

 

 

运行结果。

提交git

 

结对编程实验,结果以及提交git

 

 

 

 

计算器代码如下:

package caculater;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc extends MouseAdapter {
    JFrame list;
    // Container con;
    JTextField show;
    JButton[] jbNum = new JButton[10];
    JPanel jpMain; // 主面板
    JPanel jpRight; // 右子面板主要用于存放运算符和等号
    JPanel jpLight; // 左子面板用于存放数字,符号, “.”
    JButton dight; // 小数点
    JButton sign; // 正负号
    JButton add; // 加号
    JButton sub; // 减号
    JButton multiply; // 乘号
    JButton divide; // 除号
    JButton power; // 求幂
    JButton cos; // cos
    JButton sin; // sin
    JButton ln; // ln
    JButton ce; // 清除
    JButton equal; // 等于
    JButton mod; // 取余
    JButton sqrt; // sqrt
    double sum = 0; // 临时结果
    boolean b = false; // 监控运算符是否被点击,错误是否出现,用于实现下一次点击按钮时清空
    operator i = operator.un; // 记录等号符点击前某一运算符点击次数,用于实现连加或者连减等

    int op; // 记录操作符

// 操作符一包括+-*/%^
    enum operator {
        add, sub, mul, div, mod, pow, sin, cos, sqrt, ln, un
    }

    void display() {
        // 创建主窗口,添加一个Text框,
        list = new JFrame("计算器");
        list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        list.setSize(360, 230);
        list.setLocation(400, 300);
        list.setBackground(Color.LIGHT_GRAY); // 设置窗口背景颜色
        list.setResizable(false);

        list.setLayout(new FlowLayout(FlowLayout.CENTER));
        show = new JTextField(31);
        show.setHorizontalAlignment(JTextField.RIGHT); // 文本框内文字右对齐
        show.setEditable(false); // 文本框不可编辑
        list.add(show);
        // 创建面板并设置布局
        jpMain = new JPanel();
        jpRight = new JPanel();
        jpLight = new JPanel();
        jpMain.setLayout(new GridLayout(1, 2));
        jpRight.setLayout(new GridLayout(4, 3, 3, 3));
        jpLight.setLayout(new GridLayout(4, 3, 3, 3));
        list.add(jpMain);
        jpMain.add(jpLight);
        jpMain.add(jpRight);
        // 创建0~9按钮对象
        for (int i = 9; i >= 0; i--) {
            jbNum[i] = new JButton(String.valueOf(i));
            jbNum[i].setForeground(Color.BLUE);
            jpLight.add(jbNum[i]);
            jbNum[i].addMouseListener(this);
        }
        add = new JButton("+");
        sub = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("/");
        power = new JButton("x^y");
        sin = new JButton("sin");
        cos = new JButton("cos");
        ln = new JButton("ln");
        ce = new JButton("CE");
        equal = new JButton("=");
        mod = new JButton("%");
        sqrt = new JButton("sqrt");
        jpRight.add(divide);
        jpRight.add(sqrt);
        jpRight.add(ln);
        jpRight.add(multiply);
        jpRight.add(sin);
        jpRight.add(mod);
        jpRight.add(sub);
        jpRight.add(cos);
        jpRight.add(ce);
        jpRight.add(add);
        jpRight.add(power);
        jpRight.add(equal);

        // 给所有按钮注册监听器
        dight = new JButton(".");
        sign = new JButton("±");
        jpLight.add(sign);
        jpLight.add(dight);
        add.addMouseListener(this);
        sub.addMouseListener(this);
        multiply.addMouseListener(this);
        divide.addMouseListener(this);
        power.addMouseListener(this);
        sin.addMouseListener(this);
        cos.addMouseListener(this);
        ln.addMouseListener(this);
        ce.addMouseListener(this);
        equal.addMouseListener(this);
        mod.addMouseListener(this);
        sqrt.addMouseListener(this);
        dight.addMouseListener(this);
        sign.addMouseListener(this);
        list.setVisible(true);

    }

    public void mouseClicked(MouseEvent e) {
        // 0~9的输入
        if (e.getSource() == jbNum[0]) {
            input(0, e);
        }
        if (e.getSource() == jbNum[1]) {
            input(1, e);
        }
        if (e.getSource() == jbNum[2]) {
            input(2, e);
        }
        if (e.getSource() == jbNum[3]) {
            input(3, e);
        }
        if (e.getSource() == jbNum[4]) {
            input(4, e);
        }
        if (e.getSource() == jbNum[5]) {
            input(5, e);
        }
        if (e.getSource() == jbNum[6]) {
            input(6, e);
        }
        if (e.getSource() == jbNum[7]) {
            input(7, e);
        }
        if (e.getSource() == jbNum[8]) {
            input(8, e);
        }
        if (e.getSource() == jbNum[9]) {
            input(9, e);
        }

        // 小数点,正负号,CE,等号
        if (e.getSource() == dight) {
            if (show.getText().indexOf('.') == -1) {
                show.setText(show.getText() + ".");
            }

        }
        if (e.getSource() == sign) {
            if (show.getText().indexOf("-") == -1) {
                show.setText("-" + show.getText());
            } else {
                show.setText(show.getText().replace('-', '\0'));
            }

        }
        if (e.getSource() == ce) {
            show.setText("0");
            sum = 0;
            i = operator.un;
            b = false;
        }
        outer: if (e.getSource() == equal) {
            try {
                if (i == operator.un) {
                    b = true;
                } else {
                    if (i == operator.add) {
                        sum += Double.parseDouble(show.getText());

                    }
                    if (i == operator.sub) {
                        sum -= Double.parseDouble(show.getText());

                    }
                    if (i == operator.mul) {
                        sum *= Double.parseDouble(show.getText());

                    }
                    if (i == operator.div) {
                        if (Double.parseDouble(show.getText()) != 0) {
                            sum /= Double.parseDouble(show.getText());

                        } else {
                            show.setText("ERROR");
                            b = true;
                            sum = 0;
                            break outer; // 不执行trimIn()方法 屏幕显示错误
                        }
                    }
                    if (i == operator.mod) {
                        sum %= Double.parseDouble(show.getText());

                    }
                    if (i == operator.pow) {
                        sum = Math.pow(sum, Double.parseDouble(show.getText()));

                    }
                    trimIn(sum);
                }
            } catch (Exception ex) {
                show.setText("ERROR");
                b = true;
                sum = 0;
            }

            sum = 0;
            i = operator.un;
            b = true;
        }
        // 加减乘除//幂指函数//取余
        if (e.getSource() == add) {
            cal(i);
            i = operator.add;
            b = true;

        }
        if (e.getSource() == sub) {
            cal(i);
            i = operator.sub;
            b = true;

        }
        if (e.getSource() == multiply) {
            cal(i);
            i = operator.mul;
            b = true;

        }
        if (e.getSource() == divide) {
            cal(i);
            i = operator.div;
            b = true;

        }
        if (e.getSource() == mod) {
            cal(i);
            i = operator.mod;
            b = true;

        }
        if (e.getSource() == power) {
            cal(i);
            i = operator.pow;
            b = true;

        }

        // sqrt,sin,cos,ln
        try {
            if (show.getText() != "ERROR") {
                if (e.getSource() == sqrt) {
                    sum = Math.sqrt(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == sin) {
                    sum = Math.sin(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == cos) {
                    sum = Math.cos(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == ln) {
                    sum = Math.log(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
            }
        } catch (Exception ex) {
            show.setText("ERROR");
            b = true;
        }
    }

    // 用以四则运算和求幂和取模的方法
    public void cal(operator i) {
        try {
            if (show.getText() != "ERROR") {
                if (i == operator.un) {
                    sum = Double.parseDouble(show.getText());
                }
                if (i == operator.add) {
                    sum += Double.parseDouble(show.getText());
                    trimIn(sum);
                }
                if (i == operator.sub) {
                    sum -= Double.parseDouble(show.getText());
                    trimIn(sum);
                }
                if (i == operator.mul) {
                    sum *= Double.parseDouble(show.getText());
                    trimIn(sum);
                }
                if (i == operator.div) {
                    if (Double.parseDouble(show.getText()) != 0) {
                        sum /= Double.parseDouble(show.getText());
                        trimIn(sum);
                    } else {
                        //出现0后,把一切数据重置
                        show.setText("ERROR");
                        sum = 0;
                        b = true;
                        i=operator.un;
                    }
                }
                //取余
                if (i == operator.mod) {
                    sum %= Double.parseDouble(show.getText());
                    trimIn(sum);
                }
                //幂指函数
                if (i == operator.pow) {
                    sum = Math.pow(sum, Double.parseDouble(show.getText()));
                    trimIn(sum);
                }
            }
        } catch (Exception ex) {
            show.setText("ERROR");
            b = true;
        }
    }

    // 点击数字输入
    public void input(int i, MouseEvent e) {
        if (b == true) {
            show.setText(String.valueOf(i));
            b = false;
        } else {
            //判断0和.来清除整数时后面的点
            if (show.getText().indexOf('0') == 0 && e.getSource() != dight) {
                show.setText(String.valueOf(i));
            } else {
                show.setText(show.getText() + String.valueOf(i));
            }
        }
    }

    // sum的显示,整数的去掉小数点和0
    public void trimIn(double sum) {
        // if (show.getText().indexOf('.') != -1 &&
// show.getText().endsWith("0")) {
// show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)
// .indexOf('.'))));
// } else
        if (String.valueOf(sum).indexOf('.') != -1
                && String.valueOf(sum).endsWith("0")) {
            show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)
                    .indexOf('.'))));

        } else if (Double.isNaN(sum)) {
            show.setText("ERROR");         //不 是数字时 屏幕显示错误,并把sum置于0 运算符置UN
            b = true;
            sum = 0;
            i = operator.un;
        } else if (Double.isInfinite(sum)) {
            show.setText("ERROR");          //出现infinite(无限大)时显示错误SUM置0运算符置UN 
            b = true;
            sum = 0;
            i = operator.un;
        } else {
            show.setText(String.valueOf(sum));
        }
    }

    /**
     * @param args
*/
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Calc c = new Calc();
        c.display();
    }

}

这个代码是在我自己电脑上的,和git库中的有区别,因为实验楼中的实验环境不允许中文字符,所以我先在自己电脑上做好了之后删除中文相关注释再粘贴进实验楼、提交git的。

git地址:http://git.shiyanlou.com/20135202yjx/shiyanlou_cs212/src/master/caculator/src/caculator/Calc.java

 

我是和20135236贾瑗一起结对编程,我负责设计和代码实现,她负责产品测试。

贾瑗的博客地址是:http://www.cnblogs.com/javajy/  

 

【实验体会】

通过这次实验,我了解到了重构的妙处,在以往编写程序的时候,经常有代码不够简洁的情况,当时我只能够手动的进行修改,不仅花费较大时间,还经常出现各种小错误浪费更多时间和精力。有了重构之后可以迅速方便快捷的使代码更加层次分明,的确非常实用,但是我现在对重构的认识还不足,很多选项并不知道怎么用,需要加强学习。

代码风格这方面,我平时对缩进的格式比较在意,但是对不同功能之间代码块的分行并没有注意,反而是很喜欢把所有空行都去掉,这是个很不好的习惯,不利于代码的阅读,今后会改正。

本次试验中我还学习了git,git的版本控制在我看来很有用,能够做到undo,恢复之前某一个时间的某一个版本。在实验楼的实验环境中我完成了git的实验,但是在windows下是使用还是不熟练,需要加强。

 

 

步骤

耗时

百分比

需求分析

20min

14.2%

设计

30min

21.4%

代码实现

60min

42.9%

测试

此处由同伴负责

-

分析总结

30min

21.4%

posted @ 2015-06-05 00:14  20135202闫佳歆  阅读(402)  评论(0编辑  收藏  举报