20165304-JAVA third experiment

1. The purpose and requirements of the experiment

完成实验、撰写实验报告,注意实验报告重点是运行结果,遇到的问题以及分析
实验报告中统计自己的PSP(Personal Software Process)时间
掌握使用IDEA完成基础编程与程序调试的能力

2. Experimental content

XP Foundation
XP Core Practice
Related Tools

3. Experimental steps

Install the alibaba plugin to solve specification problems in the code

打开Settings ->Plugins -> Browse repositories...在搜索框输入alibaba即可看到Alibaba Java Code Guidelines插件,点击Install进行安装,然后重启IDEA生效
在IDEA左目录栏里右键.java文件图标,选择编码约规扫描,然后根据下方报错里修改自己的代码格式。
规范例代码截图

refactor

重命名:对类,接口,方法,属性等重命名,以使得更易理解
抽取代码:将方法内的一段代码抽取为另一个方法,以使得该段代码可以被其他方法调用,这是重构中很重要很常用的,此举可以极大的精炼代码,减少方法的代码行数
封装字段:将类的某个字段转换成属性,可以更加合理的控制字段的访问
抽取接口:将类的某些属性,方法抽取组成个接口,该类自动实现该接口
提升方法内的局部变量为方法的参数:这主要是在写代码的过程中会使用到
删除参数:将方法的一个或多个参数删掉
重排参数:将方法的参数顺序重新排列
实验截图如下:

Java and Cryptography Learning

Experience encryption and decryption---Caesar cipher

重构后的代码为:
public class Caesar{
public static void main(String []args) throws Exception{
    String s=args[0];
    int key=Integer.parseInt(args[1]);
    String es="";
    for(int i=0;i<s.length( );i++)
    {  char c=s.charAt(i);
        // 是小写字母
        c = getC(key, c);
        es+=c;
    }
    System.out.println(es);
}
private static char getC(int key, char c) {
    if(c>='a' && c<='z') {
        //移动key%26位
        c = move(key, c);
        if(c<'a') {
            //向左超界
            c = changeCplus(c);
        }
        if(c>'z') {
            //向右超界
            c = changeCminus(c);
        }
    }
    // 是大写字母
    else if(c>='A' && c<='Z') {
        c = move(key, c);
        if(c<'A') {
            c = changeCplus(c);
        }
        if(c>'Z') {
            c = changeCminus(c);
        }
    }
    return c;
}
private static char changeCminus(char c) {
    c -= 26;
    return c;
}

private static char changeCplus(char c) {
    c += 26;
    return c;
}

private static char move(int key, char c) {
    c+=key%26;
    return c;
}
}

Java Digest Algorithm - MD5

Computes the message digest for the specified string using Java.
The MessageDigest class in the java.security package provides methods for computing message digests,

First generate an object, execute its update() method to pass the original data to the object, and then execute its digest( ) method to get the message digest. Specific steps are as follows:

(1) Generate a MessageDigest object
MessageDigest m=MessageDigest.getInstance("MD5");
Analysis: Same as the KeyGenerator class in Section 2.2.1. The MessageDigest class is also a factory class, and its constructor is protected, and it is not allowed to directly use new MessageDigist( ) to create objects, but must generate MessageDigest objects through its static method getInstance( ). The parameters passed in specify the algorithm used to calculate the message digest, commonly used are "MD5", "SHA" and so on. If you are interested in the details of the MD5 algorithm, please refer to http://www.ietf.org/rfc/rfc1321.txt.

(2) Pass in the string that needs to be calculated
m.update(x.getBytes("UTF8" ));
Analysis: x is the string to be calculated, and the parameter passed in by update is a byte type or a byte type array. For String, you need to use the getBytes( ) method to generate a string array first.

(3) Calculate the message digest
byte s[ ]=m.digest( );
Analysis: Execute the digest( ) method of the MessageDigest object to complete the calculation, and the calculation result is returned by an array of byte type.

(4) Processing the calculation result
If necessary, you can use the following code to convert the calculation result s into a string.

String result="";
for (int i=0; i<s.length; i++){
   result+=Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
}

rebuild code:

import java.security.*;
public class DigestPass{
public static void main(String[ ] args) throws Exception{
    String x = getString(args[0]);
    MessageDigest m=MessageDigest.getInstance("MD5");
    m.update(x.getBytes("UTF8"));
    byte[] s = getDigest(m);
    String result="";
    for (int i=0; i<s.length; i++){
        result+= getString(s[i]).substring(6);
    }
    System.out.println(result);
}
private static String getString(byte b) {
    return Integer.toHexString((0x000000ff & b) |
            0xffffff00);
}
private static byte[] getDigest(MessageDigest m) {
    return m.digest();
}

private static String getString(String arg) {
    return arg;
}
}

Guess you like

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