20165104-java Third Experiment

Source: Internet
Author: User
Tags uppercase letter

20165104-java The third experiment first, the purpose and requirements of the experiment
    • Complete the experiment, write the experiment report, pay attention to the experiment report focus on the operation results, problems encountered and analysis
    • Experiment Report on your PSP (Personal software Process) time
    • Master the ability to use idea to complete basic programming and program debugging
Second, the experimental content XP basic XP core practice related tools three, the experimental steps to install the Alibaba plug-in, solve the code in the specification problem
    • Open Settings ... Plugins Browse repositories Enter a in the search box to alibab see the Alibaba Java Code Guidelines plug-in, click Install to install, then restart idea to take effect
    • In the idea left directory bar right-click .java file icon, select 编码约规扫描 , and then modify your code format according to the error below.
    • Canonical example code

Learn the Code menu
Use code->reformate code to format the codes

Join a project with a partner

    • Modify and edit operations
      The same as the original work on their own projects, modified to mark the main changes in the content, upload
    • Experiment:
    • Renaming: Renaming classes, interfaces, methods, attributes, etc. to make it easier to understand
    • Extracting code: Extracting a piece of code from a method into another method so that the code can be called by another method, which is very important in refactoring, is very common, which can greatly refine the code and reduce the number of lines of code in the method
    • Encapsulate field: Converts a field of a class to a property, which allows for more reasonable control over the access of the field
    • Extracting interfaces: Some of the properties of a class, method extraction, which automatically implements the interface
    • The local variable inside the lift method is the parameter of the method: This is mainly used in the process of writing the code to
    • Remove parameter: Delete one or more parameters of the method
    • Reorder parameters: Rearrange the order of the parameters of a method
    • The experiment is as follows:
Java and cryptography learning experience plus decryption---Caesar password
    • The refactored code is:
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);            is the lowercase letter 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 bit            c = Move (key, C);            if (c< ' a ') {//left bounds C = Changecplus (c);            } if (c> ' z ') {//to the right over bounds C = Changecminus (c);            }}//is the uppercase 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; }}
    • 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 a way to calculate the message digest.

The object is generated first, the update () method is executed to pass the raw data to the object, and then the Digest () method is executed to get the message digest. The steps are as follows:

(1) Generating MessageDigest objects
MessageDigest m=MessageDigest.getInstance("MD5");
Analysis: The same as the Keygenerator class in the 2.2.1 section. The MessageDigest class is also a factory class whose constructors are protected and do not allow you to create objects directly using new Messagedigist (), and you must generate MessageDigest objects through their static methods getinstance (). The arguments passed in specify the algorithm used to calculate the message digest, commonly known as "MD5", "SHA" and so on. If you are interested in the details of the MD5 algorithm, refer to Http://www.ietf.org/rfc/rfc1321.txt.

(2) Incoming string to be evaluated
m.update(x.getBytes("UTF8" ));
Analysis: X is the string that needs to be evaluated, the parameter that the update passed in is a byte type or an array of byte types, and for a string, a string array needs to be generated first using the GetBytes () method.

(3) Calculate message digest
byte s[ ]=m.digest( );
Parse: Executes the MessageDigest object's Digest () method to complete the calculation, and the result of the calculation is returned by an array of byte types.

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

String result="";for (int i=0; i<s.length; i++){       result+=Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);  }
    • Refactoring 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;    }}

Code Cloud Link

20165104-java Third Experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.