20175212童皓桢 实验三敏捷开发与XP实践实验报告

20175212童皓桢 实验三敏捷开发与XP实践实验报告

实验内容

  • XP基础
  • XP核心实践
  • 相关工具

    实验步骤

    一、Code菜单功能的研究

  • Move Line/statement Down/Up:将某行、表达式向下、向上移动一行
  • suround with:用 try-catch,for,if等包裹语句
  • comment with line/block comment:把选中它区域变成注释
  • show reformat file dialog:按照格式自动对齐
  • Optimize imports:可以优化imports,去除不必要的imports
  • Insert Live Template:插入一些记不起来的 Live Template 缩写
    输入图片说明

二、下载搭档的Complex代码并添加单元测试

1.添加搭档仓库
输入图片说明
2.产品代码

/**
 * @author Jason Tong
 * @date 2019/4/29 14:32.
 */
public class Complex {
    // 定义属性并生成getter,setter
    double RealPart;
    double ImagePart;
    // 定义构造函数
    public Complex(){}
    public Complex(double R,double I){
        ImagePart = I;
        RealPart = R;
    }

    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 str = "";
        if (ImagePart > 0)
            str =  RealPart + "+" + ImagePart + "i";
        if (ImagePart == 0)
            str =  RealPart + "";
        if (ImagePart < 0)
            str = RealPart + " " + ImagePart + "i";
        return str;
    }
    // 定义公有方法:加减乘除
    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);
    }
    Complex ComplexDiv(Complex a) {
        if(a.RealPart==0||a.ImagePart==0) {
            System.out.println("被减数不能为0");
            return new Complex();
        }

        double d = Math.sqrt(a.RealPart*a.RealPart)+Math.sqrt(a.ImagePart*a.ImagePart);
        return new Complex((RealPart*a.RealPart+ImagePart*a.ImagePart)/d,Math.round((RealPart*a.ImagePart-ImagePart*a.RealPart)/d));
    }
}

3.测试代码

import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;

public class ComplexTest extends TestCase {
    Complex complex = new Complex(1,1);
    @Test
    public void testAdd(){
        assertEquals(new Complex(4.3,4.4), complex.ComplexAdd(new Complex(3.3,3.4)));
    }
    //测试加法
    @Test
    public void testSub(){
        assertEquals(new Complex(-4.3,-3.4), complex.ComplexSub(new Complex(5.3,4.4)));
    }
    //测试减法
    @Test
    public void testMulti(){
        assertEquals(new Complex(4.0,3.0), complex.ComplexMulti(new Complex(4.0,3.0)));
    }
    //测试乘法
    @Test
    public void testDiv(){
        assertEquals(new Complex(1.0,1.0), complex.ComplexDiv(new Complex(1.0,1.0)));
        assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(1.0,0.0)));
        //assertEquals(new Complex(0.0,0.0), complex.ComplexDiv(new Complex(3,4)));
        //边缘测试
    }
    @Test
    public void testequals(){
        assertEquals(true, complex.equals(new Complex(1.0,1.0)));
    }
    //测试判断相等
}

三、重构的练习

原代码为:


class A {
  final double PI=3.1415926;// PI是常量
  public double getArea(final double r) {
     return PI*r*r;
  }
  public final void speak() {
     System.out.println("您好,How's everything here ?");
  } 
}
public class Example5_9 {
   public static void main(String args[]) {
      A a=new A();
      System.out.println("面积:"+a.getArea(100));
      a.speak();     
   }
}

对类名以及变量名进行重构
输入图片说明

进行封装
输入图片说明
重构后的代码为:

/**
 * @author Jason Tong
 * @date 2019/4/29 16:53.
 */
class Calculate {

    final double PI=3.1415926;// PI是常量
    private int r;
    public double getArea() {
        return PI* getR() * getR();
    }
    public final void speak() {
        System.out.println("您好,How's everything here ?");
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }
}
public class Example5_9 {
    public static void main(String args[]) {
        Calculate a=new Calculate();
        a.setR(10);
        System.out.println("面积:"+a.getArea());
        a.speak();
    }
}

四、用Java完成密码学内容

用java实现凯撒密码
代码为:

/**
 * @author Jason Tong
 * @date 2019/5/3 16:58.
 */
public class Caesar {
    public static void main(String args[]) throws Exception{
        String s=args[0];
        int key=Integer.parseInt(args[1]);
        Movement m=new Movement();
        int n=s.length();
        String es="";
        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            if(c >= 'a' && c <= 'z'){
                es=m.realizeMove(n,c,key,'a','z');
            }
            else if (c >= 'A' && c <= 'Z'){
                es=m.realizeMove(n,c,key,'A','Z');
            }
        }
        System.out.println(es);
    }
}
/**
 * @author Jason Tong
 * @date 2019/5/3 16:59.
 */
public class Movement {
    String es="";
    public  String  realizeMove(int n,char c,int key,char a,char b){
        //移动key%26位
        c+=key%26;
        if(c<a) {
            c+=26;
            //向左超界
        }
        if(c>b) {
            c-=26;
            //向右超界
        }
        es+=c;
        return es;
    }
}

输入图片说明

实验中遇到的问题

  • 问题一:使用alibaba插件检查代码规范性,提示缺少开发者信息
    输入图片说明
  • 解决办法一:File>Setting>Editor>File and Code Template按照下图输入开发者信息,新建类时自动添加信息。
    输入图片说明

完成代码规范
输入图片说明

  • 问题二:无法推送搭档的仓库
    输入图片说明
  • 解决办法二: 根据命令行提示,表明身份信息,即可推送
    输入图片说明

    感悟和体会

  • 学会使用Java作为工具,帮助其他学科的学习,比如密码学或是数据结构
  • 可以在仓库中添加其他成员,以便于将来的结对和团队学习
  • 血泪教训!!! 修改博客随笔时一定要及时备份!!经常记得git pull!!!

参考博客

https://blog.csdn.net/weixin_42254058/article/details/81219931

http://www.cnblogs.com/rocedu/p/4795776.html

猜你喜欢

转载自www.cnblogs.com/thz666/p/10805714.html