2017-2018-2 20165318 Experiment Report of Java object-oriented programming

Source: Internet
Author: User
Tags coding standards comment tag naming convention hosting version control system stringbuffer

2017-2018-2 20165318 Experiment Report of Java object-oriented programming experiment report I. COVER of experimental reports

Course : Java Programming Class : 1653 class name : Sun Xiaolin No. : 20165318

Instructor: Lou Jia Peng experiment Date : April 27, 2018

Experiment Time : 13:45-3:25 Experiment serial number : Experiment three

Experiment name : Agile Development and XP practice

Experimental content :

    1. XP Basics
    2. XP Core Practice
    3. Related tools

Experimental Requirements :

    1. Students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course
    2. Complete the experiment, write the experiment Report, the experiment report is published in the blog Garden, note that the experimental report is focused on the running results, the problems encountered (tool search, installation, use, program editing, debugging, running, etc.), solutions (empty methods such as "Check Network", "Ask classmates", "reading" 0 points) as well as analysis (what can be learned from it, what gains, lessons, etc.). The report can refer to the guidance of Fan Fei Dragon Teacher
    3. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.
Second, the contents and steps of the experiment

Directory
    • (i) Coding standards
      • Task one: Use the tool (code->reformate code) format code in idea and learn the function of the Code menu
    • (ii) Agile Development and XP
      • Task two: Download the complex code for partner experiment Two, add no less than three JUnit unit test cases
    • (iii) Reconstruction
      • Task three: Download your partner's code for at least three refactoring
    • (iv) Practice
      • Task four: Complete the learning of Java cryptography related content by pairing, combining refactoring, git, code standards, etc.
    • (v) Problems encountered in the experimental process and solutions
    • (vi) Experimental experience and summary
    • (vii) Code hosting
    • (eight) PSP demand analysis
    • Resources

(i) Coding standards
    • Install the Alibaba plug-in to resolve the code problem. The specific process is as follows:
    1. Open Settings ... Plugins Browse repositories

    2. In the search box alibaba , you can see the Alibaba Java Code Guidelines plugin, click Install Install, and then restart the IDE to take effect:

    3. Easy to use: Right-click on the project name and select on the pop-up menu 编码规约扫描 :

What appears:

Non-standard place, there are Chinese tips and positioning to the line, Alibaba the problem into block/critical/major three levels, some rules can be fixed one-click.

The general naming conventions in Java are:

    • To embody their own meanings.
    • Terms for packages, classes, variables
    • Method name with movable bin
    • Package name all lowercase, such as: IO,AWT
    • The first letter of the class name should be capitalized, such as: Helloworldapp
    • The first letter of the variable name is lowercase
      , such as: UserName
    • The first letter of the method name is lowercase: setName

Code format after specification

import java.util.concurrent.Callable;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));        }    }}

Back to Catalog

Task one: Use the tool (code->reformate code) format code in idea and learn the function of the Code menu

The Code menu in idea is as follows:

Some of the more commonly used features are summarized below:

    • Override Methods(Ctrl+o): Method of overloading the base class;

    • Implement Methods(Ctrl+i): The method that completes the interface of the current class implements (or abstract basic Class);
      Generate (Alt+insert): Create getter and Setter methods for any field inside the class;

    • Surround With(ctrl+alt+t): Use If-else, Try-catch, Do-while and other packaging code snippets;

    • Insert Live Template(CTRL-J): Perform some of the Live Template abbreviations that cannot be remembered;

    • Comment with Line Comment(Ctrl + Slash)/ Comment with Block Comment (ctrl+shift+ Slash): Use CTRL + Slash and ctrl-shift-/to annotate (or counter-annotate) lines of code and blocks of code. Use CTRL + Slash-line comment mark ("//... ") to annotate (or counter-annotate) the current line or the selected block of code. The ctrl+shift+ slash is used to enclose the selected block with a block comment tag ("/* * * *"). To counter-annotate a block of code, press ctrl+shift+ slash anywhere in the block;

    • Reformat Code(ctrl+alt+l): Indent code in standard format;

    • show reformat file dialog: auto-align according to format

    • Optimize imports: Can optimize imports, remove unnecessary imports

    • Move Line/statement Down/Up: Moves a row, an expression, down, and up one row

Back to Catalog

(ii) Agile Development and XP

Pairing programming is an important practice in XP. In pair programming mode, a pair of programmers work side-by-shoulder, equally, and in complementary way. They sit in front of a computer, facing the same monitor, working with the same keyboard and the same mouse. They analyze together, design together, write test cases together, encode together, do unit tests together, do integration testing together, write documents together, and so on.
--quoting from experimental three agile development and XP practice

Task two: Download the complex code for partner experiment Two, add no less than three JUnit unit test cases

The complex code for your partner is as follows:

The methods to be tested are:,, ComplexAdd() ComplexSub() ComplexMulti() , ComplexDiv() ;
Write the test code and upload it to your partner Code cloud project:

Import Junit.framework.testcase;import org.junit.test;import Static org.junit.assert.*;/** * Created by sxx on 2018/4/    */public class Complextest extends TestCase {Complex C1 = new Complex (0.0, 2.0);    Complex C2 = new Complex (-1.0,-1.0);    Complex C3 = New Complex (1.0,2.0);        @Test public void Testgetrealpart () throws exception{Assertequals (0.0,complex.getrealpart (0.0));        Assertequals ( -1.0,complex.getrealpart (-1.0));    Assertequals (1.0,complex.getrealpart (1.0));        } @Test public void Testgetimagepart () throws exception{Assertequals (2.0,complex.getimagepart (2.0));        Assertequals ( -1.0,complex.getimagepart (-1.0));    Assertequals (2.0,complex.getimagepart (2.0)); } @Test public void Testcomplexadd () throws exception{assertequals (" -1.0+1.0i", C1.        Complexadd (C2). toString ()); Assertequals ("1.0+4.0i", C1.        Complexadd (C3). ToString ()); Assertequals ("0.0+1.0i", C2.    Complexadd (C3). ToString ()); } @Test Publicvoid Testcomplexsub () throws exception{assertequals ("1.0+3.0i", C1.        Complexsub (C2). toString ()); Assertequals (" -1.0", C1.        Complexsub (C3). ToString ()); Assertequals (" -2.0-3.0i", C2.    Complexsub (C3). ToString ()); } @Test public void Testcomplexmulti () throws exception{assertequals ("2.0-2.0i", C1.        Complexmulti (C2). toString ()); Assertequals (" -4.0+2.0i", C1.        Complexmulti (C3). ToString ()); Assertequals ("1.0-3.0i", C2.    Complexmulti (C3). ToString ()); } @Test public void Testcomplexdiv () throws exception{assertequals (" -1.0-1.0i", C1.        Complexdiv (C2). toString ()); Assertequals ("0.4+0.8i", C1.        Complexdiv (C3). ToString ()); Assertequals (" -0.6-0.6i", C2.    Complexdiv (C3). ToString ()); }}

Back to Catalog

(iii) Reconstruction

Refactoring (refactor) is to change the structure within the software without changing the external behavior of the software, making it easier to read, easy to maintain, and easy to change.

We want to modify the software, original aim, is nothing more than four kinds of motives:

    • Add new features;
    • The original function has a bug;
    • Improving the structure of the original procedures;
    • Optimize the performance of legacy systems.

Where refactoring is required:
Code duplication, methods too long, parameter columns too long, conditional logic overly complex, branch statements

A complete refactoring process consists of:

    1. Check out code from the version control system library
    2. Read code (including test code)
    3. Find Bad smell
    4. Refactoring
    5. Run all the unit Tests
    6. Check in code to codebase

Task three: Download your partner's code for at least three refactoring

There are several issues with the above code:

    • The class name does not match the naming convention;
    • The variables in the class are not encapsulated;
    • methods are not encapsulated;

Program changes into the following:

Back to Catalog

Practice

Task four: Complete the learning of Java cryptography related content by pairing, combining refactoring, git, code standards, etc.

The Java security architecture consists of 4 parts:

    • JCA (Java cryptography Architecture, Java Cryptographic architecture): JCA provides basic cryptographic frameworks such as certificates, digital signatures, message digests, and key pair generator.

    • JCE (Java cryptography Extension, Java Encryption extension package): JCE is extended on the basis of JCA, providing various cryptographic algorithms, message digest algorithms, and Key management functions. The implementation of JCE is mainly in the Javax.crypto package (and its child packages)

    • JSSE (Java secure Sockets Extension, Java secured Sockets extension Pack): JSSE provides encryption based on SSL (secure Sockets layer, secured sockets Layer). In the transmission of the network, the information will pass through multiple hosts (most likely one of them will be tapped), eventually transmitted to the recipient, which is not safe. This service to ensure the security of network communications is provided by Jsse.

    • Jaas (Java Authentication and authentication service, Java authentication and security services): Jaas provides the ability to authenticate users on the Java platform.

After learning, I and my partner chose to implement the Caesar password encryption and decryption algorithm, and in a pair of ways to complete the code writing work.

Caesar cipher algorithm: Encrypt the letters in the alphabet by moving them in a certain position.

  • Specific steps:
    (1) According to Caesar cipher characteristic design algorithm
    (2) Incoming string to be evaluated
    (3) Processing calculation results

  • Pseudo code:

    定义main方法传入参数参数判断,是否有非法输入,若有,抛出异常进行加解密,大小写区分输出结果
  • Product Code:
    Caesaar.java

    public class Caesar {public static void main (string[] args) throws Eadexception {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); Custom Exception class Try {Boolean exist = ((C > a) && (C < 91)) | |            ((C >) && (C < 123) | | c = = + | | c = = 33);            if (exist = = false) {throw new eadexception (s);        }} catch (Eadexception e) {System.out.println (e.warnmess ());            }//lowercase letter if (c >= ' a ' && c <= ' z ') {//move key%26 bit c + = key% 26;            To Zou bounded if (c < ' a ') {c + = 26;            }//Right Hyper-bounds if (C > ' z ') {c-= 26;            }}//capital letter else if (c >= ' A ' && C <= ' Z ') {c + = key% 26;                if (C < ' A ') {c + = 26;            } if (C > ' Z ') {c-= 26;    }} es + = c; } System.out.println (es);}}

Eadexception.java

public class EadException extends Exception{    String message;    public EadException(String sourceString){        message = "The input is not correct";    }    public String warnMess(){        return message;    }}

Initial code:

Operation Result:

Code specification:

Refactoring:

In addition to implementing the Caesar cipher algorithm, I have also run other algorithms:

Des algorithm

    • Keys generated in file Key1.dat

    • Save key encoding and print

    • Encrypt with Desede

    • Decrypt with Desede.

RSA encryption

RSA decryption

MD5 algorithm

Back to Catalog

(v) Problems encountered in the experimental process and solutions
    • Q1: in the reconstruction of the relevant content of the experiment, because the content of the refactoring is not very understanding, the code of the small partners are mostly book code, find a lot, have not found too much need to refactor the place.

    • Solution: in learning the experiment three agile Development and XP practice--Lou Teacher's blog, as well as reference to the Java code refactoring several modes of explanation
      After the Java Code Refactoring (series), I looked at the partner's code again and found several places that could be regulated.

    • Q2: When compiling the MD5 algorithm, the following issues occur

    • Workaround: through Alibaba encoding scan, I found that there is a place in the code ";" The ";", which is used in Chinese, causes a coding error, and after correcting the error, the code can run normally.

Back to Catalog

(vi) Experimental experience and summary

Through this experiment, I mainly learned the code standard, code refactoring, Java cryptography algorithm and so on.

Code standards and refactoring of learning, so I notice that I wrote the code before the many of the nonstandard, and by using Alibaba real-time encoding scanning, so that I write code at the same time, pay attention to the canonical code of the standard format, so that the written code more clear, the class name and variable name specification, also let me write code , and a lot of unnecessary comments are reduced.

With the previous arithmetic knot pair programming exercise, in the course of this experiment, I cooperate with my small partners more harmonious, save some unnecessary time, pair programming efficiency has a certain improvement.

Back to Catalog

Code Hosting

My Code Cloud Address
Partner's Code cloud address

PSP Requirements Analysis
Steps Time Consuming percentage
Demand analysis 10min 6%
Design 20min 11%
Code implementation 70min 60g
Test 20min 13%
Analysis Summary 50min 30%

Resources

Java Cryptography Algorithm--Lou Teacher's Blog
Experiment three agile Development and XP practice--Lou Teacher's Blog
Several patterns of Java code refactoring
Java Code refactoring (series explained)
IntelliJ Idea Use Tips checklist
Back to Catalog

2017-2018-2 20165318 Experiment Report of Java object-oriented programming

Related Article

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.