Java's second experiment--java object-oriented programming

Source: Internet
Author: User

Java's second experiment--java object-oriented programming

Beijing Institute of Electronic Technology

Lab Report

Course: Java Programming Class: 1352 name: Linhan Kam No.: 20135213

Score: Instructor: Lou Jia Peng Experimental Date: 2015.4.14

Experiment level: Preview degree: Experiment time: 19:30~21:50

Instrument Group: 13 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. 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 instrument:

Name

Model

Number

Pc

Acer

1

Virtual machines

Laboratory Building

1

First, the contents of the experiment

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

Second, the experimental steps

(a) Unit testing

(1) Three kinds of code

Programming is intellectual activity, not typing, what to do before programming, how to do to think clearly to write the program, write well. With a lot of students at present, a program to open the editor to write code, I hope that students develop a habit, when you want to solve problems with the procedure, you will write three kinds of code:

• Pseudo Code

• Product Code

• Test Code

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:

    • 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

(ii) Object-oriented three elements

(1) Abstract

Abstract the meaning of the word refers to people in the cognitive thinking activities in the object of the appearance of the factors of abandonment and the essence of the extraction of factors . Abstract is a kind of thinking tool that people often use when they know complex things and phenomena, abstract ability of abstract thinking is very important in program design,"refine, simplifying, youbiaojili and seeking the same in different ways" has a great extent to determine the programmer's programming ability.
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. Object-oriented thinking involves all aspects of software development, such as object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP). OOA decomposes The system according to the abstract key problem domain and focuses on what. Ood is an object-oriented implementation of the symbolic design system, which constructs the system into a "real-world" object in a way that is very close to the problem domain terminology, and focuses on how to implement the functional specification through the model. OOP is coded in a programming language (such as Java) on a design basis. The main line that runs through OOA, Ood, and OOP is abstraction. Ood Modeling uses the graphical Modeling language UML (Unified Modeling Language), UML is a generic modeling language, we experiment with Umbrello for modeling, and Windows recommends that you use STARUML.

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 inherited and polymorphic mechanisms. Data abstraction is the core and origin of OOP.

The first element of the OO three elements is encapsulation, which is the encapsulation of data and related behaviors to be wrapped together to implement information hidden. Java uses classes for encapsulation, such as a dog class:

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. 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 code below to test the code and run the results such as: (Attached to the number. I really do not understand the requirements code how to express, direct folder)

(iii) Preliminary design pattern

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

It is very difficult to use the three elements, especially polymorphism, with the help of abstract thinking, and the S.O.L.I.D design principle is a good guide:

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)

There are many other object-oriented principles beyond the solid principle. Such as:

    • "Combinatorial substitution Inheritance": this is to say that relative to inheritance, more inclined to use the combination;
    • "The Law of the Piper": This means "the less you know about other classes, the better";
    • "Common closure principle": This means that "related classes should be packaged together";
    • "Stable abstract principle": This means that "the more stable the class, the more it should be composed of abstract classes";

Of course, these principles are not isolated, but closely linked, followed by one principle and followed by one or more principles, contrary to one of the principles is also likely to violate the other one or more principles. Design patterns are the result of the application of these principles in some specific scenarios. Therefore, the design pattern can be regarded as "framework" and the Ood principle as "norm". In the process of learning the design pattern, we should constantly reflect on it, which embodies the principle of object-oriented design.

2. Mode and design mode

A pattern is a customary solution (solution) for a particular problem (problem) under an external environment (context). The pattern must make the problem clear, explain why it is used to solve the problem, and under what circumstances it will not work. Each pattern can be reused because of its repeatability, has its own name, can be imparted, and can be transplanted into different scenarios. Patterns can be seen as expert solutions to a problem that can be reused.

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

(Attached number:)

3. Design Pattern Real Example

(1) Design pattern provides a subsystem or component for refining a software system, or a diagram between them.

It describes the public representation structure of communication components, which can solve a design problem in a particular context.

Behind the design pattern is the principle of abstraction and solid.

(2) There are 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 (trade-offs)

(3) Understand the design patterns that may exist over design issues and how to avoid them.

(iv) Practice

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

Pseudo code:

First, a complex class complex is designed to define three construction methods:

① The default is real and imaginary parts are 0 when there is no parameter;

② a parameter defaults to a real number, that is, the imaginary part is 0,

③ two parameters for both real and imaginary parts

Then define two member methods to calculate the sum of two complex numbers and the difference. Defines a print () method that outputs a complex number and does not output imaginary parts when the imaginary part is 0.
Finally, a song class is defined using the complex class, in which two complex objects are created in the main method of the class, respectively, and the sum and difference of the two complex numbers are computed and output.

Product Code:

(previously written in the experimental building, running slowly, and then directly in their own computer run, the experimental building in Chinese can not be lost, so the previous change with pinyin)

Run:

Third, the experimental analysis

1. My PSP Time

Steps

Take

Percentage

Demand analysis

5

10%

Design

10

20%

Code implementation

24

50%

Test

5

10%

Analysis Summary

5

10%

2, encountered problems and solutions:

(1) Look for various icons, because the icons are small and difficult to find.

Resolution: The discovery Point opens to see a larger view.

(2) Eclipse hints Animal Code is wrong, and dog and cat need to be defined in the file.

Solution: Put animal and dog and cat in different class files.

(3) Because the network signal is not good, the virtual machine runs very slowly, so that this experiment is very time-consuming

Solution: The experimental process is written in the virtual machine, and the exercises are modified directly with your own computer.

3. Summary of Feelings

When using the program to solve the actual problem, it is best to write three kinds of code: pseudo-code, product code, test code, such a program has practical significance, convenient to use.

The standalone unit of the software will be tested in isolation from the rest of the program, looking for errors, writing high-quality code, and improving programming levels.

This also allows code to be assured of modification and refactoring, allowing programmers to design software modules from the perspective of the caller rather than the creator.

Java's second experiment--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.