其他分享
首页 > 其他分享> > 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

作者:互联网

实验三 敏捷开发与XP实践-1

1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这里展开了。
2.安装了alibaba 插件,点击左上角的File,然后点击第五个选项Settings,弹出了一个选项框,在左侧点击Plugins,在上面的Marketplace搜索alibaba,然后就可以下载了,下载之后会要求重启IDEA。

3.在IDEA中重新格式化代码。
public class CodeStandard { public static void main(String [] args){ StringBuffer buffer = new StringBuffer(); buffer.append('S'); buffer.append("tringBuffer"); System.out.println(buffer.charAt(1)); System.out.println(buffer.capacity()); System.out.println(buffer.indexOf("tring")); System.out.println("buffer = " + buffer.toString()); if(buffer.capacity()<20) buffer.append("1234567"); for(int i=0; i<buffer.length();i++) System.out.println(buffer.charAt(i)); } }

但是不清楚为什么,复制到我的IDEA之后,就差不都格式化了。

还是按照要求在IDEA中使用工具(Code->Reformate Code)把代码重新格式化,发现了一些不同。

4.研究Code菜单

在网上查了一些Code菜单中的选项功能。
| 选项 | 快捷键 |功能 |
| --- | --- | --- |
|Override Methods |Ctrl + O | 重写覆盖方法 |
| Implements Methods | Ctrl + I | 实现接口方法|
| Gengrate | Alt + Insert | 生成Construct、Getter/Setter、toString等 |
| Surround With | Ctrl + Alt +T | 生成包围代码 |
| Unwarp/Remove | Ctrl + Shift + Delete | 取消代码包围 |
| Reformat Code | Ctrl + Alt + L | 格式化代码 |
| Move Statement Down |Ctrl + Shift + ↓ | 方法下移 |
| Move Statement Up | Ctrl + Shift + ↑ |方法上移 |
| Move Line Down | Alt + Shift + ↓ | 代码行下移 |
| Move Line Up | Alt + Shift + ↑ | 代码行上移 |

例子:Move Line
将鼠标移动至需要换行的代码行,单击左键,会发现该行会被选定。

使用快捷键或者点击Code菜单,完成换行。

实验三 敏捷开发与XP实践-2

要求:在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;提交搭档项目git log的截图,包含上面git commit的信息,并加上自己的学号水印信息。

搭档20175333Complex.java代码:

import java.text.DecimalFormat;

public class Complex {
    double RealPart;
    double ImagePart;

    public Complex() {

    }

    public Complex(double R, double I) {
        RealPart = R;
        ImagePart = I;
    }

    public void setter(double R, double I) {
        RealPart = R;
        ImagePart = I;
    }

    public double getterRealPart() {
        return RealPart;
    }

    public double getterImagePart() {
        return ImagePart;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String s = "";
        if (getterRealPart() == 0 && getterImagePart() == 0) {
            s = "0";
        } else if (getterRealPart() != 0 && getterImagePart() == 0) {
            s = s + getterRealPart();
        } else if (getterRealPart() == 0 && getterImagePart() != 0) {
            s = s + getterImagePart() + "i";
        } else {
            if (getterImagePart() < 0) {
                s = s + getterRealPart() + getterImagePart() + "i";
            } else {
                s = s + getterRealPart() + "+" + getterImagePart() + "i";
            }

        }
        return s;
    }

    public Complex ComplexAdd(Complex a) {
        return new Complex((getterRealPart() + a.getterRealPart()), (getterImagePart() + a.getterImagePart()));
    }

    public Complex ComplexSub(Complex a) {
        return new Complex((getterRealPart() - a.getterRealPart()), (getterImagePart() - a.getterImagePart()));
    }

    public Complex ComplexMulti(Complex a) {
        double imaginePart = getterRealPart() * a.getterImagePart() + getterImagePart() * a.getterRealPart();
        double realPart = getterRealPart() * a.getterRealPart() - getterImagePart() * a.getterImagePart();
        return new Complex(realPart, imaginePart);
    }

    public Complex ComplexDiv(Complex a) throws Exception {
        double imaginePart = -getterRealPart() * a.getterImagePart() + getterImagePart() * a.getterRealPart();
        double realPart = getterRealPart() * a.getterRealPart() + getterImagePart() * a.getterImagePart();
        double div = a.getterRealPart() * a.getterRealPart() + a.getterImagePart() * a.getterImagePart();
        if (div == 0) {
            throw new Exception();
        }
        realPart = realPart / div;
        imaginePart = imaginePart / div;
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        return new Complex(Double.valueOf(decimalFormat.format(realPart)), Double.valueOf(decimalFormat.format(imaginePart)));
    }
}

测试代码ComplexTest

import junit.framework.TestCase;
import org.junit.Test;

public class ComplexTest extends TestCase {
    Complex complex = new Complex(34,56);
    Complex complex2 = new Complex();
    public void testGetterRealPart() {
        assertEquals(34.0,complex.getterRealPart());
        complex.setter(0,-1);
        assertEquals(0.0,complex.getterRealPart());
        complex.setter(-3.6,5);
        assertEquals(-3.6,complex.getterRealPart());
    }

    public void testGetterImagePart() {
        assertEquals(56.0,complex.getterImagePart());
        complex.setter(34,-57.233);
        assertEquals(-57.233,complex.getterImagePart());
        complex.setter(4,0);
        assertEquals(0.0,complex.getterImagePart());
    }

    public void testEquals() {
        assertTrue(complex.equals(complex));
        Complex complex2 = new Complex();
        assertFalse(complex.equals(complex2));
    }

    public void testToString() {
        complex.setter(0,0);
        assertEquals("0",complex.toString());
        complex.setter(0.2,3);
        assertEquals("0.2+3.0i",complex.toString());
        complex.setter(4.5,0);
        assertEquals("4.5",complex.toString());
        complex.setter(0,6);
        assertEquals("6.0i",complex.toString());
        complex.setter(-5.6,-6.7);
        assertEquals("-5.6-6.7i",complex.toString());
    }

    public void testComplexAdd() {
        complex2 = complex.ComplexAdd(new Complex(-33,-57));
        assertEquals("1.0-1.0i",complex2.toString());
        assertEquals("33.0+57.0i",complex2.ComplexAdd(complex).toString());
    }

    public void testComplexSub() {
        complex2 = complex.ComplexSub(new Complex(33,57));
        assertEquals("1.0-1.0i",complex2.toString());
        assertEquals("-33.0-57.0i",complex2.ComplexSub(complex).toString());
    }

    public void testComplexMulti() {
        complex.setter(-3.5,5);
        complex2.setter(0,0);
        assertEquals("0",complex.ComplexMulti(complex2).toString());
        complex2.setter(10,-3);
        assertEquals("-20.0+60.5i",complex.ComplexMulti(complex2).toString());
    }
    @Test(expected = Exception.class)
    public void testComplexDiv() throws Exception{
        complex.setter(3,3);
        complex2.setter(0,9);
        assertEquals("0.3-0.3i",complex.ComplexDiv(complex2).toString());
        complex.setter(0,0);
        assertEquals("0",complex.ComplexDiv(complex2).toString());
    }
}

实验三 敏捷开发与XP实践-3

重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更 。
重构后的代码:

import java.text.DecimalFormat;

public class Complex {
    double realPart;
    double imagePart;

    public Complex() {

    }

    public Complex(double r, double i) {
        realPart = r;
        imagePart = i;
    }

    public void set(double r, double i) {
        realPart = r;
        imagePart = i;
    }

    public double getRealPart() {
        return realPart;
    }

    public double getImagePart() {
        return imagePart;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String toString() {
        String s = "";
        if (getRealPart() == 0 && getImagePart() == 0) {
            s = "0";
        } else if (getRealPart() != 0 && getImagePart() == 0) {
            s = s + getRealPart();
        } else if (getRealPart() == 0 && getImagePart() != 0) {
            s = s + getImagePart() + "i";
        } else {
            if (getImagePart() < 0) {
                s = s + getRealPart() + getImagePart() + "i";
            } else {
                s = s + getRealPart() + "+" + getImagePart() + "i";
            }

        }
        return s;
    }

    public Complex complexAdd(Complex a) {
        return new Complex((getRealPart() + a.getRealPart()), (getImagePart() + a.getImagePart()));
    }

    public Complex complexSub(Complex a) {
        return new Complex((getRealPart() - a.getRealPart()), (getImagePart() - a.getImagePart()));
    }

    public Complex complexMulti(Complex a) {
        double imaginePart = getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() - getImagePart() * a.getImagePart();
        return new Complex(realPart, imaginePart);
    }

    public Complex complexDiv(Complex a) throws Exception {
        double imaginePart = -getRealPart() * a.getImagePart() + getImagePart() * a.getRealPart();
        double realPart = getRealPart() * a.getRealPart() + getImagePart() * a.getImagePart();
        double div = a.getRealPart() * a.getRealPart() + a.getImagePart() * a.getImagePart();
        if (div == 0) {
            throw new Exception();
        }
        realPart = realPart / div;
        imaginePart = imaginePart / div;
        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        return new Complex(Double.valueOf(decimalFormat.format(realPart)), Double.valueOf(decimalFormat.format(imaginePart)));
    }
}

标签:20175307,getterImagePart,double,complex,Complex,2018,2019,getterRealPart,public
来源: https://www.cnblogs.com/gsc20175307/p/10802842.html