20165104-JAVA third experiment

20165104-JAVA third experiment

1. The purpose and requirements of the experiment

  • Complete the experiment, write the experiment report, pay attention to the experiment report focusing on the operation results, the problems encountered and the analysis
  • Count your own PSP (Personal Software Process) time in the experimental report
  • Master the ability to use IDEA to complete basic programming and program debugging

2. Experimental content

XP Basics

XP Core Practices

Related tools

3. Experimental steps

Install the alibaba plugin to solve specification problems in the code

  • Open Settings-> Plugins-> Browse repositories...Enter a in the search box alibabto see the Alibaba Java CodeGuidelines plugin, click Installto install, then restart IDEA to take effect
  • .javaRight-click the file icon in the IDEA left directory bar , select 编码约规扫描it, and then modify your code format according to the error below.
  • Specification example code screenshot

Learn the Code menu
Use Code->Reformate Code to format the code

Join a project with a partner

  • Modification and editing operations
    are the same as the original operations for your own projects.
  • Experimental screenshot:

refactor

  • Rename: rename classes, interfaces, methods, properties, etc. to make it easier to understand
  • Extract code: Extract a piece of code in a method into another method, so that this piece of code can be called by other methods, which is very important and commonly used in refactoring, which can greatly refine the code and reduce the code of the method Rows
  • Encapsulation field: Convert a field of a class into an attribute, which can control the access of the field more reasonably
  • Extract interface: extract some attributes and methods of a class to form an interface, and the class automatically implements the interface
  • The local variables in the promotion method are the parameters of the method: this is mainly used in the process of writing code
  • Delete parameters: delete one or more parameters of the method
  • Rearrange Parameters: Rearrange the order of parameters of a method
  • The screenshot of the experiment is as follows:

Java and Cryptography Learning

Experience encryption and decryption---Caesar cipher

  • The refactored code is:
    ````
    /**
  • @Date 2018/4/28
  • @author wangyaojia
    */
    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') {
    //move key% 26 bits
    c = move(key, c);
    if(c<' a') {
    //Left overrun
    c = changeCplus(c);
    }
    if(c>'z') {
    //Right overrun
    c = changeCminus(c);
    }
    }
    // Capital letter
    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;
    }
    }
    ```
  • Screenshot of the experimental results:

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 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)
m.update(x.getBytes("UTF8" ));
Analysis of incoming strings to be calculated: x is the string to be calculated, and the parameter passed in by update is a byte type or an array of byte types. For strings, 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.*;
/**

  • Demo class
  • @author zyx
  • @date 2018/04/28
    */
    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;
    }
    }
    ```

Code cloud link

Guess you like

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