Experiment two Java object-oriented programming 20135321

Source: Internet
Author: User

Course: Java Programming Class: 1353 Name: Yu Jiayuan No.: 20135321

Score: Instructor: Lou Jia Peng Experimental Date: 2015-5-8

Experiment level: Preview degree: Experiment time: 3:15~6:45

Instrument Group: Compulsory/Elective: Elective experiment number: 2

Experiment Name: Java Object-oriented programming

Experimental purposes and requirements:

1. Initial mastery of unit testing and TDD

2.

3. Initial mastery of UML modeling

4. Familiarize yourself with the S.O.L.I.D Principles Editor Course

5. Understanding Design Patterns

6. students who do not have a Linux base are advised to start with the Linux basics (new version) Vim Editor course

7. 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" and so on all get 0 points) and analysis (from which can get what revelation, what harvest, lessons, etc.).

8. Plagiarism is strictly forbidden, and the results of the experiment of the perpetrator are zero, and other punitive measures are added.            Find, 9. Please first in the laboratory building in the ~/code directory with their own school number to build a directory, code and UML diagram to put in this directory, no study number will require to redo, and then follow the following steps to practice.

Experimental instrument:

Name

Model

Number

Laptop

Acer Aspire v5-473g

1

PS: There is really not enough speed to control the experimental building, in their own computer to do.

Experimental content One

(a) Unit testing

(1) Three kinds of code

What to do before programming, how to do to think clearly before you write the program and write well. To use the program to solve the problem, you will write three kinds of code:

Pseudo code

Product Code

Test code

Requirements: We want to solve a percentile grade in a Myutil class 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:

We translate pseudo-code in the Java language, which is the available product code

The test code used is as follows

Test one: Select a valid input value to test

Test results

Test two: Test again-10 This error data, found that the result of running-10 is incorrect

Test three: Boundary test, i.e. 0,60,70,80,90,100

The test results are

Only 60 is divided into the example:

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

Write the test code first, then write the product Code development method called "Test-driven Development" (TDD). The general steps for TDD are as follows:

L CLEAR the current function to be completed and record it as a test list

L quickly completed writing test cases for this function

L Test Code compilation does not pass (no product code)

L Write Product Code

L Test Pass

L refactor the code and ensure the test passes (refactoring the next lab session)

L Cycle complete development of all functions

Take the procedure as an example

The test results showed a green bar, indicating that the test passed. The goal of TDD is "clean Code that Works", and TDD's slogan is "Keep the bar Green, to Keep the Code clean".

(iii) Object-oriented three elements

1. Abstraction

Abstraction is a matter of extracting the essential features of things and not considering their details for the time being. For complex system problems, we can solve problems by means of hierarchical abstraction. At the highest level of abstraction, the solution of the problem is described in a generalized way, using the language of the problem environment. In the lower layers of abstraction, they are described in a procedural way. When describing a problem solution, use a problem-oriented and implementation-oriented terminology. In program design, abstraction consists of two aspects, one is process abstraction and the other is data abstraction.

2. Encapsulation, inheritance and polymorphism

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

The result of a process abstraction is a function, and the result of a data abstraction is an abstract data type.

Encapsulation actually uses methods to hide the data of the class, controlling the user's modifications to the class and the extent to which the data is accessed, resulting in the benefits of modularity (modularity) and information hiding (information hiding) Interface (interface) is an accurate description of the package.

Umbrello Diagram for example

Encapsulation-based, inheritance can implement code reuse, it should be noted that inheritance more important role is to achieve polymorphism. Object-oriented objects allow different classes of objects to respond to the same message, which means that the same message can be used in a variety of different ways depending on the sending object, and we call this behavior polymorphic. In Java, polymorphism refers to a phenomenon in which different class objects execute different code when they invoke the same signed member method. Polymorphism is the basis of the flexibility and extensibility of object-oriented programming.

(iv) Preliminary design pattern

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

SRP (Single Responsibility Principle, sole responsibility principle)

OCP (open-closed Principle, open-closed principle)

LSP (Liskov substitusion Principle,liskov substitution principle)

ISP (Interface segregation Principle, interface separation principle)

DIP (Dependency inversion Principle, dependency inversion principle)

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 behavior of the extended open for Extension software module must be extensible, when the application needs change or needs to meet the new application needs, we have to let the module work in different ways, the modification is closed (Closed for modification Requires that the source code of the module is not modifiable, and no one is allowed to modify the existing module.

Based on the OCP, using the object-oriented polymorphism (polymorphic) to more flexibly handle change embracing changes, the OCP can be implemented in the following ways: Abstraction and inheritance and interface-oriented programming.

(2) mode and design mode

A pattern is the usual solution to a particular problem (problem) under an external environment (context). One of the most important is design patterns.

grasp Mode

• Analysis Mode

• Software Architecture Patterns

• Design Patterns: Created, structured, behavioral

• Management mode: The manager Pool implementation mode

• Interface Design Interaction mode

(3) design pattern Real Example

Four basic elements of the design pattern

Pattern Name: Description mode, easy to communicate, archive

Problem: Describes where to apply the pattern

Solution: Describes a design's constituent elements, not for special cases

Consequence: The results and tradeoffs of applying this pattern

Design patterns provide a subsystem or component for refining a software system, or a diagram between them, which describes the public rendition structure of a communication component, which can solve a design problem in a particular context.

We use examples to learn a design pattern (abstract Factory mode) and to understand the design patterns that may exist over-design problems and how to avoid them. We have designed a document system, such as a UML class

The corresponding code is as follows:

Results

If the customer requests the system to support the float class, this is a reasonable requirement, to support the float class, the document class to modify two places, which violates the OCP principle, using polymorphism can solve some problems:

To support the float class, the document class modifies the construction method, which violates the OCP principle. Encapsulation, inheritance, polymorphism can not solve the problem, then need to design patterns:

Modified Code

(v) Practice

Use TDD to design the complex to implement the plural class.

Pseudo code:

L The real part of the complex number is 0 and the imaginary part is 0

The real part of the complex is 0, and the imaginary part is the corresponding number.

L The real part of the plural is the corresponding number, the real part is 0

L Two parameters are real part and imaginary part respectively

L realize addition, subtraction and multiplication of two complex numbers.

Test code

Product Code

Test data and running results

Summary time:

PSP Time Statistics

Statistical error, total 3.5 hours.

Steps

Take

Percentage

Demand analysis

25mins

12%

Design

30mins

14.3%

Code implementation

120mins

57.1%

Test

30mins

14.3%

Analysis Summary

5mins

2.3%

Summarize the benefits of unit testing:

1. In unit testing, the independent unit of the software is designed to find errors by testing the code in isolation from the rest of the program, thus writing higher-quality product codes and improving the level and skill of programming.

2. Unit test code can be used to perform real data-based testing in the compilation and runtime environment through the rollback between simple program code lines, without worrying about generating unnecessary data and destroying the test results.

3. More clearly the purpose of development, to avoid duplication or excessive lengthy work, improve the productivity of developers.

4. Separating the product code from the test code allows programmers to write software modules that are easy to test and invoke, thereby facilitating the use of the code under test, thus enabling a part of the functionality to be understood by the user.

Resources

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

Tools

1. JUnit

2. Umbrello

3. StarUML

Experiment two Java object-oriented programming 20135321

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.