Java Lab Report II: Java Object-oriented programming

Source: Internet
Author: User

Java Lab Report II: Java Object-oriented programming

Experimental requirements:

1. Initial mastery of unit testing and TDD2. Understand and master 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 content

(i) Unit testing (ii) object-oriented three elements (iii) design pattern preliminary (iv) exercise

Experimental process

(a) Unit test (1) three codes

Writing a program requires the use of pseudo-code, product code, test code.

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

Definition: Write first 测试代码 , then write 产品代码 the development method

The general steps 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

Writing and debugging results of TDD mode test code

(ii) Object-oriented three elements (1) abstract

Abstract ability refers to the ability of "refine, simplifying, Youbiaojili, and seeking for the same in different".

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. Includes object-oriented analysis (OOA), Object-oriented design (OOD), object-oriented programming implementation (OOP).

OOA focuses on whatto do, and Ood focuses on how to doit, and OOP encodes it in a programming language (such as Java) on a design basis.

1. Encapsulation

Definition: Wrapping data together with related behaviors to implement information is hidden.

Encapsulation actually uses methods to hide the data of the class, controlling the user's modification of the class and the degree of access to the data, resulting in the benefits of modularity (modularity) and information hiding (information hiding)interface (interface) is an accurate description of the package. Dogclasses use classes and access control (Private,public) to hide Properties color , open interfaces setColor() , getColor() bark() and toString .

Use UML software to design the dog, cat, and animal classes and implement them in eclipse.

2. Modeling Language UML

UML is a common modeling language, we use Umbrello to model in our experiments, we recommend that you use STARUML in Windows.

The result of a process abstraction is a function, and the result of the data abstraction is an abstract data type (ADT), which can be used as an ADT with inheritance and polymorphic mechanisms. Data Abstraction is the core and origin of OOP.

Use UML software to model the animal test program and implement it through eclipse.

(iii) design pattern preliminary (1) S.O.L.I.D principle

1. SRP (single Responsibility Principle, sole responsibility principle)

Content: Never have more than one reason to modify a class. objects provide a high degree of encapsulation of a single responsibility, and object changes depend only on the change of a single responsibility.

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

Content: Software entities (classes, modules, functions, etc.) should be open to expansion, closed to modifications.

For the expansion and opening, the behavior of the software module must be extensible, when the application needs change or needs to meet the new application requirements, we should let the module work in different ways;

To modify the closed, requires the module source code is not modifiable, no one is allowed to modify the existing module source.

OCPImplementation means: (1) abstraction and inheritance, (2) interface-oriented programming.

3. LSP (Liskov substitusion Principle,liskov substitution principle)

Content: Subclasses must be able to be replaced by their base classes; Using a pointer to a base class or a referenced function must be able to use it without knowing the specific derived class object type.

The core idea is that the parent type object can be overridden by the quilt type object.

4, ISP (Interface segregation Principle, interface separation principle)

Content: Customers should not rely on interfaces that they do not use (not too many functions of the interface)

5, DIP (Dependency inversion Principle, dependency inversion principle)

Content: High-level modules should not be dependent on the lower layers, both should be dependent on abstraction; abstractions should not be dependent on detail, and detail should be dependent on abstraction.

(2) mode and design mode

pattern is an external environment, the usual solution to a specific problem, can be seen as a problem can be reused expert solution .

(3) design pattern Real Example

1. Four basic elements:

Mode Name: Description mode, easy to communicate, archive

Problem: Describes where to apply the pattern

Workaround: Describe the constituent elements of a design, not for special cases

Result: The results and tradeoffs of applying this pattern

2. Abstract Factory mode

Prone to over-design problems

Just for the plastic data.

(iv) Exercise 1 using TDD to design a complex that implements a complex number class. 2. Report the time of your PSP (Personal software Process)
Steps Take Percentage
Demand analysis
Design
Code implementation
Test
Analysis Summary

(1) Pseudo-code:

1. A = A (real number) + bi (imaginary part);
2. Add

The real part and the real number are added together, the imaginary part and the imaginary part are added.
3. Subtraction

Subtraction of real and real parts, partial subtraction of imaginary part and imaginary part
4. Multiply
a*b=ac-bd+ (AD+BC) I
A=a+bi, B=c+di
5. Display a plural
The imaginary part is negative, and the preceding plus sign is not displayed

(2) Product Code:

———— not completed ————

Java Lab Report II: Java Object-oriented programming

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.