20165304 Experiment 3

The cover of the lab report

Course: Java Programming Class: Class 1653

Name: Li Songyang Student ID: 20165304

Instructor: Lou Jiapeng Experiment Date: April 28, 2018

Experiment time: 15:35 - 17:15 Experiment number: 3

Experiment Name: Agile Development and XP Practice

2. Experimental content

  • Agile Development and XP Practice-1

    Experiment requirements: Use the tool (Code->Reformate Code) in IDEA to reformat the following code, and then study the Code menu to find a function that makes you feel the best. Submit a screenshot and add your student ID watermark.

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)); } }

experiment procedure:

An important realization in writing code is that "programs are mostly meant for people to see", and programming standards make code easier to read and understand, and even guarantee fewer bugs in it.

Code layout does not affect the functionality of the program, but it does affect readability. The layout of the program pursues clarity and beauty, which is an important factor in the style of the program.

We can download the Alibaba Java Code Guidelines plugin in Idea to make our writing more elegant.

We can install it through the official Jetbrains repository: Open Settings -> Plugins -> Browse repositories...

Enter alibaba in the search box to see the Alibaba Java Code Guidelines plugin, click Install to install it, and then restart IDEA to take effect.

Code can be formatted in Code. The code before formatting looks like this:

After formatting it looks like this:

The functions in Code are more than that, Override Methods, Implement Methods, Surround With, etc. I think the best use is Surround With, which can use if-else, for, do-while, etc. to decorate the code.

  • Agile Development and XP Practice-2

Experimental requirements:

Add your learning partner to your own project on the code cloud. After confirming that the partner's project is added to yourself, download the Complex code of partner experiment 2, add no less than three JUnit unit test cases, and git add .; git after the test is successful commit -m "Add content to your student number"; git push;

Submit a screenshot of the partner project git log, including the above git commit information, and add your own student number watermark information.
The code is shown in the figure

The methods that can be unit tested are ComplexAdd(), ComplexSub(), ComplexMulti(), ComplexDiv().

The screenshot of running the unit test is:

  • Agile Development and XP Practice-3

    Experiment requirements: Complete the refactoring exercises, download the code of your partner, perform at least three refactorings, submit a screenshot of the refactored code, and add your own student ID watermark. Submit your partner's code cloud project link.
    Experimental process: Refactoring refers to changing the internal structure of the software to make it easier to read, maintain and change without changing the external behavior of the software.
    In fact, it is simpler to make the program more standardized without changing the essence of the program.
    There is a function menu in Idea that is used for refactoring:

Download the code as

The problems with this code are: the class name does not conform to the coding standard, too many variables are defined, and the judgment statement is too long.

after refactoring to

  • Agile Development and XP Practice - 4

    experiment procedure:

My partner and I are working on the implementation of the Caesar cipher. The encryption and decryption algorithms of the Caesar cipher are relatively simple.
The encryption process is as follows:

If the plaintext is recorded as m, the ciphertext is recorded as c, the secret key is k, the encryption transformation is recorded as Encrypt(k,m), and the decryption transformation is recorded as Decrypt(k,m)). The encryption process of the Caesar cipher can be recorded as the following transformation:

c≡m+k mod n (where n is the number of basic characters)

Similarly, the decryption process can be expressed as:
m≡c+k mod n (where n is the number of basic characters)

Our encryption code is
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
    System.out.print("请输入明文:");
    Scanner a=new Scanner(System.in);
    String b=a.nextLine();
    System.out.print("请输入秘钥:");
    Scanner c=new Scanner(System.in);
    int key=c.nextInt();
    Encrypt(b, key);
}

public static void Encrypt(String str,int k){
    String s="";
    for (int i = 0; i < str.length(); i++) {
        char c=str.charAt(i);
        if(c>='a'&&c<='z'){
            c+=k%26;
            if(c<'a')
                c+=26;
            if(c>'z')
                c-=26;

        }else if(c>='A'&&c<='Z'){
            c+=k%26;
            if(c<'A')
                c+=26;
            if(c>'Z')
                c-=26;
        }
        s+=c;
    }
    System.out.println(str+" 加密为: "+s);
}
}

The decryption code is:

import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
    System.out.print("请输入密文:");
    Scanner a=new Scanner(System.in);
    String b=a.nextLine();
    System.out.print("请输入秘钥:");
    Scanner c=new Scanner(System.in);
    int key=c.nextInt();
    Decrypt(b, key);
}

public static void Decrypt(String str,int k){
    String s="";
    for (int i = 0; i < str.length(); i++) {
        char c=str.charAt(i);
        if(c>='a'&&c<='z'){
            c-=k%26;
            if(c<'a')
                c+=26;
            if(c>'z')
                c-=26;

        }else if(c>='A'&&c<='Z'){
            c-=k%26;
            if(c<'A')
                c+=26;
            if(c>'Z')
                c-=26;
        }
        s+=c;
    }
    System.out.println(str+" 解密为: "+s);
}
}

screenshot

Experimental perception

Through this experiment, I realized that some of the previously written code was very irregular and corrected.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325008291&siteId=291194637