20165232 实验三

20165232《Java程序设计》实验三(敏捷开发与XP实践)实验报告

实验报告封面

指导教师:娄嘉鹏 实验日期:2018年4月30日
实验时间:15:45 - 17:20
实验序号:实验三
实验名称:敏捷开发与XP实践

实验内容:

  1. 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题。

在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能。提交截图,加上自己学号水印。

  1. 在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二的Complex代码,加入不少于三个JUnit单元测试用例,测试成功后git add .; git commit -m "自己学号 添加内容";git push;
  2. 完成重构内容的练习,下载搭档的代码,至少进行三项重构,提交重构后代码的截图,加上自己的学号水印。提交搭档的码云项目链接。
  3. 参考 http://www.cnblogs.com/rocedu/p/6683948.html,以结对的方式完成Java密码学相关内容的学习,结合重构,git,代码标准

实验步骤

(一)alibaba 插件与Code菜单

Move Line/statement Down/Up:将某行、表达式向下、向上移动一行

suround with:用 try-catch,for,if等包裹语句

comment with line/block comment:把选中它区域变成注释

show reformat file dialog:按照格式自动对齐

Optimize imports:优化imports

Insert Live Template:插入 Live Template 缩写

image

(二)Complex代码添加测试用例

结对伙伴代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;
public class SizeYunsuan {

    /**
     * @param args
     */
    public static Random rand=new Random();
    public static class Qst
    {
        static int Operand(int Range)//产生操作数
        {
            int Opd=rand.nextInt(Range*2+1)-Range;
            return Opd;
        }
        public static char OperatorKind(char Operator)//生成运算符
        {
            int OperatorPossible=rand.nextInt(3);

            switch(OperatorPossible)
            {
                case 0:
                    Operator='+';
                    break;
                case 1:
                    Operator='-';
                    break;
                case 2:
                    Operator='*';
                    break;

                default:
                    System.out.print("Error!");
            }
            return Operator;
        }
        public static boolean IfRepeated(String str[],int Location)//判断是否重复
        {
            for(int i=0;i<Location;i++)
            {
                if(str[i].equals(str[Location]))
                    return true;
            }
            return false;
        }
        public static int Ans(int ans,int Operand1,int Operand2,char Operator)//生成答案
        {
            switch(Operator)
            {
                case '+':
                    ans=Operand1+Operand2;
                    break;
                case '-':
                    ans=Operand1-Operand2;
                    break;
                case '*':
                    ans=Operand1*Operand2;
                    break;


                default:
                    System.out.print("Error!");
            }
            return ans;
        }
        //生成一道运算题
        public static void CreateStr(int Range,char Operator,String str[],int i,int QstNum,int ans[])
        {       int answer = 0;
                Qst.OperatorKind(Operator);
                int Operand1=Qst.Operand(Range);
                int Operand2=Qst.Operand(Range);
                str[i]=Integer.toString(Operand1);
                str[i]+=Operator;
                str[i]+=Integer.toString(Operand2);
                str[i]+="=";
                while(IfRepeated(str,i))//判断是否重复
                {
                    Operand1=Qst.Operand(Range);
                    Operand2=Qst.Operand(Range);
                    str[i]=Integer.toString(Operand1);
                    str[i]+=Operator;
                    str[i]+=Integer.toString(Operand2);
                    str[i]+="=";
                }
                    ans[i]=Qst.Ans(answer,Operand1,Operand2,Operator);
        }
        public static void Display(String str[])//输出生成的运算题
        {

            for(int j=0;j<str.length;j++)
            {
                System.out.print(str[j]);
                if(j%4==3)
                {
                    System.out.println();
                }
                else
                {
                    System.out.print('\t');
                }
            }
        }
        public static void Writefile(String str[],String filePath){

            try {
                 File file = new File(filePath);
                 PrintStream ps = new PrintStream(new FileOutputStream(file));
                 for(int j=1;j<str.length;j++){
                 ps.println(j+"、"+str[j]);// 往文件里写入字符串

                 }

            } catch (FileNotFoundException e) {
                // TODO: handle exception
                 e.printStackTrace();
            }
        }
        public static void Input(int Input[],int QstNum)//输入问题答案
        {
            Scanner sca=new Scanner(System.in);
            for(int j=0;j<QstNum;j++)
            {
                Input[j]=sca.nextInt();
            }
        }
        public static void OutAns(int ans[],int QstNum)//输出答案
        {
            for(int j=0;j<QstNum;j++)
            {
                System.out.print(ans[j]);
                if(j%4==3)
                {
                    System.out.println();
                }
                else
                {
                    System.out.print('\t');
                }
            }
        }
        public static void ConfirmAns(int ans[],int Input[],int QstNum,int count)
        {   int a[] = new int[QstNum];
            count=0;
            for(int i=0;i<QstNum;i++)
            {
                if(ans[i]==Input[i]){
                    count++;
                    a[i]=i;
                }else{
                    a[i]=-1;
                }
            }
            try {
                File file = new File("D:\\Grade.txt");
                PrintStream ps = new PrintStream(new FileOutputStream(file));
                System.out.print("Correct:");
                ps.print("Correct:");
                System.out.print(count);
                ps.print(count);
                System.out.print("(");
                ps.print("(");
                for(int i=0;i<a.length;i++){

                    if(a[i]!=-1){

                         System.out.print(i+1+",");
                         ps.print(i+1+",");
                        }
                    }

                System.out.println(")");
                 ps.println(")");
                System.out.print("Wrong:");
                ps.print("Wrong:");
                System.out.print(QstNum-count);
                ps.print(QstNum-count);
                System.out.print("(");
                ps.print("(");
                for(int i=0;i<a.length;i++){

                    if(a[i]==-1){

                         System.out.print(i+1+",");
                         ps.print(i+1+",");
                        }
                    }

            System.out.println(")");
            ps.println(")");

            } catch (FileNotFoundException e) {
                // TODO: handle exception
            }

        }


    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int Range,QstNum=0,count=0;

        char Operator = '+';
        Scanner sca=new Scanner(System.in);
        System.out.println("请输入生成题目的数量:");
        QstNum=sca.nextInt();
        System.out.println("请输入算数范围:");
        Range=sca.nextInt();

        String str[] = new String[QstNum];
        int ans[]=new int[QstNum];
        int Input[]=new int[QstNum];
        for( int i=0;i<QstNum;i++)
        {
            try
            {
                Qst.CreateStr(Range,Qst.OperatorKind(Operator),str,i,QstNum,ans);
            }
            catch(Exception e)
            {
                i--;
            };
        }
        Qst.Display(str);
        String filePath;
        filePath="D:\\Exercises.txt";
        Qst.Writefile(str, filePath);
        System.out.println();
        System.out.println("输入答案:");
        Qst.Input(Input, QstNum);
        System.out.println("正确答案:");
        Qst.OutAns(ans,QstNum);
        System.out.println();
        Qst.ConfirmAns(ans,Input,QstNum,count);
    }

}

运行通过的截图

image

搭档项目git log的截图

image

(三)重构

  1. Rename可以给类、包、方法、变量改名字,增加代码的可读性。

image

  1. Refactor->Encapsulate Field...用来封装成员变量

image

  1. Source->Generate toString()...用来产生一个toString方法

(四)以结对方式完成Java密码学的学习

凯撒密码的加密过程可记为如下一个变换:

c≡m+k mod n (其中n为基本字符个数)

同样,解密过程可表示为:

m≡c+k mod n (其中n为基本字符个数)

image

重构后,将Code,Rename为Caesar,并为p赋初值0

image

实验体会

通过此次实验,我明白了代码规范性的重要,也学习了一些基础的规范代码的操作。

PSP

步骤 耗时 百分比
需求分析 25min 8%
设计 75min 25%
代码实现 150min 50%
测试 12.5min 4%
分析总结 37.5min 13%

猜你喜欢

转载自www.cnblogs.com/heyanda/p/8965871.html