20135119_ Shanwen Experiment two Java object-oriented programming

Source: Internet
Author: User

Beijing Institute of Electronic Technology (BESTI)

Lab Report

Course: Java class: 1351 Name: Shanwen No.: 20135119

Score: Instructor: Lou Jia Peng Experimental Date: 2015.4.17

Experiment level: Preview degree: Experiment time: 15:30~18:00

Instrument Group: Compulsory/Elective: Elective experiment serial number: (i)

Experiment Name: Java Object-oriented programming

Experiment Objective: 1. Initial mastery of unit testing and TDD

2. Understanding and mastering the object-oriented three elements: encapsulation, inheritance, polymorphism

3. Initial mastery of UML modeling

4. Familiarity with S.O.L.I.D principles

5. Understanding Design Patterns

Experimental requirements: 1. Students who do not have a Linux base are advised to start with theLinux Basics (new edition)Vim Editor course

2. Complete the experiment, write the experiment Report, the experiment report is published in the blog site Blog, note that the experimental report is focused on the results of the operation, problems encountered (tool search, installation, use, program editing, commissioning, operation, etc.), solutions (empty Hole methods such as "Check Network", "Ask classmates", "reading" and so on all get 0 points) and analysis (from which can get what revelation, what harvest, 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.

4. please first in the laboratory building in the ~/code directory with their own school number to create a directory, code and UML diagram to put in this directory, no study number will be required to redo, and then follow the following steps to practice.

Experimental steps:

(a) Unit testing

(1) Three kinds of code

take the results as an example: we want to solve a percentile grade in a Myutil class to turn into "excellent, good, medium, pass, fail" five grade system performance function.

· Pseudo code

/*

          Percentile five-point system:

If the score is less than 60, turn into "fail"

If the score is between 60 and 70, turn into "pass"

If the score is between 70 and 80, turn into "medium"

If the score is between 80 and 90, turn into "good"

If the score is between 90 and 100, turn into "good"

Other, turn into "error"

*/

• Product Code

         

Maybe worry about some bugs, then we have to write the test code.

• Test Code

Test code requires more than one set, and all of the circumstances are tested again, as in the case.

        

Test failed 1, indicating that myutil code checked out a bug, now to modify Myutil.java code

        

think this is the end? You're wrong, general code is the most error-prone at the boundary, we haven't tested the boundary, and we've tested the boundary conditions that are entered as "0,60,70,80,90,100" as follows:

        

You can see that another bug has occurred, and then continue to modify Myutil.java, the code is as follows:

        

The results of the test were passed.

When we make Myutil.java available to others, we have a good heart. How do you ensure that the unit measure is adequate? Our general requirement is to test the code more than the product code. How to write the test, "the way of unit test" put forward a right-bicep method, we can refer to. Software is done by the cooperation of many people, and the work of different people is dependent on each other. Many of the errors in the software come from the programmer's misunderstanding of the function of the module, negligence or the failure to understand the module changes. How can I be responsible for the module function definition as clear as possible, the internal changes of the module will not affect the other modules, and the quality of the module can be stable, quantitative guarantee? Unit testing is a very effective solution.

(2) TDD (test driven devlopment, testing-driven development)

The simple understanding is: Write the test code first, write the product code, this test method is called "Test-driven Development" (TDD).

The general steps of *TDD are as follows:

    • Clear the current functionality to be completed and record it as a test list
    • Quick completion of writing test cases for this feature
    • Test code compilation does not pass (no product code)
    • Write the Product Code
    • Test Pass
    • Refactor the code and ensure the test passes (refactoring the next lab session)
    • Cycle through the development of all functions

There is a unit test tool JUnit in Java to aid in TDD, and we use TDD to rewrite the example of the previous percentile to five-point system, and realize the benefits of developing with the test tool support.

The steps are as follows:

(1) Open Eclipse, click File->new->java Project to create a new Tdddemo Java project, put the mouse on the project name Tdddemo, right-click, select new->sour in the popup menu Ce folder create a new test catalog

    

(2) We put the mouse on the test directory, right-click, select the New->junit test case in the pop-up menu to create a new test cases class myutiltest.

  

Note: we add the first test case testnormal, note that the test case must be annotated @Testbefore the test case method name is arbitrary.

(3) First write Myutiltest will certainly appear error, because there is no product code, how to test, the next is to write the product code.

(4) We put the mouse on the Myutiltest.java, right-click, select Run As->junit Test.

(5) The test results showed a red bar, indicating that the test did not pass, the test results showed a green bar, the test passed.

(6) Continue to modify the Myutil.java code, after testing to get:

This completes a huge project.

Regardless of TDD, writing high-quality test cases is the most important, how to do unit testing, you can refer to the "Unit test of the road" this book. In addition, "Agile Java Chinese version" shows how to integrate Java and TDD effectively, through TDD Drive project development, interested can be consulted.

(ii) Object-oriented three elements

(1) Abstract: The abstract ability of "refine, simplifying, youbiaojili and seeking the same in different" is very important to the programmer's program design ability.

  

(2) encapsulation, inheritance and polymorphism

The three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism.

The dog class hides the attribute color by using class and access control (private,public), opening the interface SetColor (), GetColor (), bark (), and ToString. The Dog class is a module that we can use with the following code to test the code and run the results as follows:

  

(3) We can use the class diagram in UML to describe the class dog, first we open the shell in the environment of the experimental building, enter Umbrello in the command line, open the UML modeling software Umbrello.

The model after UML modeling is as follows:

As we can see, in UML, the property of a class can display its name, type, initialization value, and properties can also display private,public,protected. The methods of the class can display their method names, parameters, return types, and the Private,public,protected property of the method. which

    • + indicates public
    • #表示 protected
    • -Indicates private

We see that both the dog and cat classes have the color attribute and the corresponding setter and getter methods, which clearly violate the dry principle mentioned earlier, and we can solve this problem through inheritance. Place the Color property and the corresponding setter and getter methods in the parent class animal, as shown in the following UML:

(iii) Preliminary design pattern

(1) S.O.L.I.D principle

Object-oriented three elements are "encapsulation, inheritance, polymorphism", and any object-oriented programming language will support these three elements syntactically. It is very difficult to use the three elements, especially polymorphism, to s.o.l.i.d the design principle of the class of abstract.

OCP is one of the most important principles in Ood, and the OCP's content is:

    • Software entities (class, modules, function, etc) should open for extension,but closed for modification.
    • Software entities (classes, modules, functions, etc.) should be open to expansion and closed to modifications.

The SRP content is:

    • There should never be + than one reason for a class to change
    • Never have more than one reason to modify a class

The LSP content is:

    • Subtypes must is substitutable for their base types
    • Functions that use pointers or references to base classes must is able to use objects of derived classes without knowing I T
    • The subclass must be able to be used by its base class
    • Using a pointer to a base class or a referenced function, you must be able to use it without knowing the specific derived class object type
    • The content of the ISP is: clients should not being forced to depend upon interfaces, they do

Customers should not rely on interfaces that they do not use

The content of the dip is:

    • The modules should not depend upon low level modules. Both should depend upon abstractions
    • Abstractions should not depend upon details. Details should depend upon abstractions
    • High-level modules should not be dependent on low-layer modules. Both should be dependent on the abstract

Abstraction should not depend on detail. Details should depend on abstraction

(2) mode and design mode

There are many patterns in computer science:

    • Grasp mode
    • Analysis mode
    • Software Architecture Patterns
    • Design Patterns: Created, structured, behavioral
    • Management mode: The manager Pool implementation mode
    • Interface Design Interaction Mode
    • ...

The most important thing here is the design pattern, the position of design pattern in object-oriented can be equal to the position of data structure in process programming.

(iv) Exercise: Use TDD to design a complex that implements a complex number class.

Pseudo code:

1, a plural class is designed, and the real and imaginary parts of the complex numbers are represented by variables.

2, the addition and subtraction of complex numbers are realized by adding and decreasing the real part and imaginary part respectively.

3, print the plural in the plural format.

Test code:

Product Code:

2. Report the time of your PSP (Personal software Process)

steps

Time-consuming

percent

Requirements analysis

 60min

 

< p> design

60min 

 

Code implementation

 60min

 

Test

 60min

 

Analysis Summary

 60mim

&NBSP;

3. Benefits of Unit Testing:

(1) can make the compiled code more rational.

(2) The problems found in unit tests are easy to find.

(3) Can reduce unnecessary time-consuming.

V. References

1. UML Essentials 2. "The Law of Construction (electronic version)", author Xin Zou blog 3. "The design pattern of the Chinese language" 4. "Analytic Extreme Programming" 5. "The way of unit testing"

    1. "Code Encyclopedia"
    2. The abstract three principles of code
    3. Junit
    4. Umbrello
Vi. Tools

StarUML

20135119_ Shanwen Experiment two 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.