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

一、实验内容与步骤

1.安装、使用alibaba 插件规范代码

  • 在IDEA的setting中找到plugins并搜索alibaba,点击install进行安装
  • 重启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));}
    }
}

2.任务一:使用工具(Code->Reformate Code)格式化代码,并了解code菜单的功能

  • 使用工具(Code->Reformate Code)格式化代码
    • code中选择Reformate Code格式化代码
  • code菜单中的功能
    • Override Methods(ctrl+O):重载基本类的方法

    • Surround With(Ctrl+Alt+T):使用if-else、for、while等语句包装代码段(这是我觉得最好用的功能之一)

    • Comment with Line Comment(Ctrl+/):将本行变成注释

3.任务二:下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例

  • 搭档的Complex代码如下:
import java.lang.Integer;
import java.util.Objects;

public class Complex {
    //定义属性并生成getter,setter
    double RealPart;
    double ImagePart;
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }

    //定义构造函数
    public Complex(){
        RealPart = 0;
        ImagePart = 1;
    }
    public Complex(double R,double I){
        RealPart = R;
        ImagePart = I;
    }

    //Override Object
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Complex)) {
            return false;
        }
        Complex complex = (Complex) obj;
        if(complex.RealPart != ((Complex) obj).RealPart) {
            return false;
        }
        if(complex.ImagePart != ((Complex) obj).ImagePart) {
            return false;
        }
        return true;
    }
    public String toString(){
        String s = new String();
        if (ImagePart > 0){
            s = getRealPart() + "+" + getImagePart() + "i";
        }
        if (ImagePart == 0){
            s = getRealPart() + "";
        }
        if(ImagePart < 0){
            s = getRealPart() + "" + getImagePart() + "i";
        }
        if(RealPart == 0){
            s = getImagePart() + "i";
        }
        if (RealPart==0&&ImagePart==0){
            s="0";
        }
        return s;
    }
    //定义公有方法:加减乘除
    Complex ComplexAdd(Complex a){
        return new Complex(RealPart + a.RealPart,ImagePart + a.ImagePart);
    }
    Complex ComplexSub(Complex a){
        return new Complex(RealPart - a.RealPart,ImagePart - a.ImagePart);
    }
    Complex ComplexMulti(Complex a){
        return new Complex(RealPart*a.RealPart-ImagePart*a.ImagePart,RealPart*a.ImagePart + ImagePart*a.RealPart);
    }
    Complex ComplexDiv(Complex a) {
        return new Complex((RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart), (ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart));
    }
}
  • 要测试的类getRealgetImaginarytoStringequalscomplexAdd、complexSub、complexMulti、complexDiv```

  • 测试代码如下:

import junit.framework.TestCase;

public class Complex_pTest extends TestCase {
    Complex_p a=new Complex_p(2,3);
    Complex_p b=new Complex_p(0,1);
    Complex_p c=new Complex_p(-1,-2);
    public void testGetReal() {
        assertEquals(2.0,a.getReal());
        assertEquals(0.0,b.getReal());
        assertEquals(-1.0,c.getReal());
    }

    public void testGetImaginary() {
        assertEquals(3.0,a.getImaginary());
        assertEquals(1.0,b.getImaginary());
        assertEquals(-2.0,c.getImaginary());
    }

    public void testToString1() {
        assertEquals("2.0+3.0i",a.toString());
        assertEquals("1.0i",b.toString());
        assertEquals("-1.0-2.0i",c.toString());
    }
    public void testComplexAdd() {
        assertEquals("2.0+4.0i",a.ComplexAdd(b).toString());
        assertEquals("1.0+1.0i",a.ComplexAdd(c).toString());
        assertEquals("-1.0-1.0i",b.ComplexAdd(c).toString());
    }

    public void testComplexSub() {
        assertEquals("2.0+2.0i",a.ComplexSub(b).toString());
        assertEquals("1.0+3.0i",b.ComplexSub(c).toString());
        assertEquals("-3.0-5.0i",c.ComplexSub(a).toString());
    }

    public void testComplexMulti() {
        assertEquals("-3.0+2.0i",a.ComplexMulti(b).toString());
        assertEquals("4.0-7.0i",a.ComplexMulti(c).toString());
        assertEquals("2.0-1.0i",b.ComplexMulti(c).toString());
    }

    public void testComplexDiv() {
        assertEquals("-1.4-4.0i",a.ComplexDiv(c).toString());
        assertEquals("-0.2-1.0i",b.ComplexDiv(c).toString());
    }
}

然后将测试代码传到搭档的码云里,git log如下:

4.任务三:重构

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

  • 修改软件的四种动机:

    • 增加新功能;
    • 原有功能有BUG;
    • 改善原有程序的结构;
    • 优化原有系统的性能 。
  • 需要重构的地方:

    • 代码重复;
    • 方法过长;
    • 参数列过长;
    • 条件逻辑过度复杂;
    • 分支语句
  • 一个完整的重构流程应包括

    • 从版本控制系统代码库中Check out code
    • 读懂代码(包括测试代码)
    • 发现bad smell
    • Refactoring
    • 运行所有的Unit Tests
    • 往代码库中Check in code
  • 重构1:添加作者和日期

  • 重构2:对类中的变量和方法进行封装

  • 重构3:规范方法名(首字母小写)

  • 修改后的代码

/*
* @author dwt
* @date 2019/5/3
 */
import java.lang.Integer;
import java.util.Objects;

public class Complex {
    //定义属性并生成getter,setter
    private double RealPart;
    private double ImagePart;
    public double getRealPart(){
        return RealPart;
    }
    public double getImagePart(){
        return ImagePart;
    }

    //定义构造函数
    public Complex(){
        RealPart = 0;
        ImagePart = 1;
    }
    public Complex(double R,double I){
        RealPart = R;
        ImagePart = I;
    }

    @Override
    public boolean equals(Object obj){
        if(this == obj){
            return true;
        }
        if(!(obj instanceof Complex)) {
            return false;
        }
        Complex complex = (Complex) obj;
        if(complex.RealPart != ((Complex) obj).RealPart) {
            return false;
        }
        if(complex.ImagePart != ((Complex) obj).ImagePart) {
            return false;
        }
        return true;
    }
    @Override
    public String toString(){
        String s = new String();
        if (ImagePart > 0){
            s = getRealPart() + "+" + getImagePart() + "i";
        }
        if (ImagePart == 0){
            s = getRealPart() + "";
        }
        if(ImagePart < 0){
            s = getRealPart() + "" + getImagePart() + "i";
        }
        if(RealPart == 0){
            s = getImagePart() + "i";
        }
        if (RealPart==0&&ImagePart==0){
            s="0";
        }
        return s;
    }
    //定义公有方法:加减乘除
    Complex complexAdd(Complex a){
        return new Complex(RealPart + a.RealPart,ImagePart + a.ImagePart);
    }
    Complex complexSub(Complex a){
        return new Complex(RealPart - a.RealPart,ImagePart - a.ImagePart);
    }
    Complex complexMulti(Complex a){
        return new Complex(RealPart*a.RealPart-ImagePart*a.ImagePart,RealPart*a.ImagePart + ImagePart*a.RealPart);
    }
    Complex complexDiv(Complex a) {
        return new Complex((RealPart * a.ImagePart + ImagePart * a.RealPart) / (a.ImagePart * a.ImagePart + a.RealPart * a.RealPart), (ImagePart * a.ImagePart + RealPart * a.RealPart) / (a.RealPart * a.RealPart + a.RealPart * a.RealPart));
    }
}

4.任务四:Java密码学相关内容的学习

二、代码托管

我的码云链接
搭档的码云链接

三、参考资料

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

posted @ 2019-05-03 17:17  20175215丁文韬  阅读(160)  评论(0编辑  收藏  举报